<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>SpringMVC - 肉蛋葱鸡肉粥🍛的博客☕</title><meta name="Description" content=""><meta property="og:title" content="SpringMVC" />
<meta property="og:description" content="[TOC] 一、SpringMVC简介 1、什么是MVC MVC是一种软件架构的思想，将软件按照模型、视图、控制器来划分 M：Model，模型层，指工程中的" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/posts/springmvc/" /><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2021-06-30T10:31:53+08:00" />
<meta property="article:modified_time" content="2021-06-30T10:31:53+08:00" /><meta property="og:site_name" content="肉蛋葱鸡肉粥🍛的博客☕" />

<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="SpringMVC"/>
<meta name="twitter:description" content="[TOC] 一、SpringMVC简介 1、什么是MVC MVC是一种软件架构的思想，将软件按照模型、视图、控制器来划分 M：Model，模型层，指工程中的"/>
<meta name="application-name" content="肉蛋葱鸡肉粥🍛的博客☕">
<meta name="apple-mobile-web-app-title" content="肉蛋葱鸡肉粥🍛的博客☕"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/posts/springmvc/" /><link rel="prev" href="http://example.org/posts/mysql/" /><link rel="next" href="http://example.org/posts/htmlcss%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "SpringMVC",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/posts\/springmvc\/"
        },"genre": "posts","keywords": "java, SpringMVC, 模块化规范","wordcount":  20542 ,
        "url": "http:\/\/example.org\/posts\/springmvc\/","datePublished": "2021-06-30T10:31:53+08:00","dateModified": "2021-06-30T10:31:53+08:00","publisher": {
            "@type": "Organization",
            "name": "yzuxqz"},"author": {
                "@type": "Person",
                "name": "yzuxqz"
            },"description": ""
    }
    </script></head>
    <body header-desktop="" header-mobile=""><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="肉蛋葱鸡肉粥🍛的博客☕">肉蛋葱鸡肉粥🍛的博客☕</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/about/"> 关于 </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item language" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                        <select class="language-select" id="language-select-desktop" onchange="location = this.value;"><option value="/posts/springmvc/" selected>简体中文</option></select>
                    </a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="肉蛋葱鸡肉粥🍛的博客☕">肉蛋葱鸡肉粥🍛的博客☕</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/about/" title="">关于</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a><a href="javascript:void(0);" class="menu-item" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                    <select class="language-select" onchange="location = this.value;"><option value="/posts/springmvc/" selected>简体中文</option></select>
                </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">SpringMVC</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://github.com/yzuxqz" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>yzuxqz</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/java/"><i class="far fa-folder fa-fw"></i>java</a>&nbsp;<a href="/categories/%E5%90%8E%E7%AB%AF/"><i class="far fa-folder fa-fw"></i>后端</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-06-30">2021-06-30</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 20542 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 42 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1什么是mvc">1、什么是MVC</a></li>
        <li><a href="#2什么是springmvc">2、什么是SpringMVC</a></li>
        <li><a href="#3springmvc的特点">3、SpringMVC的特点</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1开发环境">1、开发环境</a></li>
        <li><a href="#2创建maven工程">2、创建maven工程</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a添加web模块">a&gt;添加web模块</a></li>
                <li><a href="#b打包方式war">b&gt;打包方式：war</a></li>
                <li><a href="#c引入依赖">c&gt;引入依赖</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#3配置webxml">3、配置web.xml</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a默认配置方式">a&gt;默认配置方式</a></li>
                <li><a href="#b扩展配置方式">b&gt;扩展配置方式</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#4创建请求控制器">4、创建请求控制器</a></li>
        <li><a href="#5创建springmvc的配置文件">5、创建springMVC的配置文件</a></li>
        <li><a href="#6测试helloworld">6、测试HelloWorld</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a实现对首页的访问">a&gt;实现对首页的访问</a></li>
                <li><a href="#b通过超链接跳转到指定页面">b&gt;通过超链接跳转到指定页面</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#7总结">7、总结</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1requestmapping注解的功能">1、@RequestMapping注解的功能</a></li>
        <li><a href="#2requestmapping注解的位置">2、@RequestMapping注解的位置</a></li>
        <li><a href="#3requestmapping注解的value属性">3、@RequestMapping注解的value属性</a></li>
        <li><a href="#4requestmapping注解的method属性">4、@RequestMapping注解的method属性</a></li>
        <li><a href="#5requestmapping注解的params属性了解">5、@RequestMapping注解的params属性（了解）</a></li>
        <li><a href="#6requestmapping注解的headers属性了解">6、@RequestMapping注解的headers属性（了解）</a></li>
        <li><a href="#7springmvc支持ant风格的路径">7、SpringMVC支持ant风格的路径</a></li>
        <li><a href="#8springmvc支持路径中的占位符重点">8、SpringMVC支持路径中的占位符（重点）</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1通过servletapi获取">1、通过ServletAPI获取</a></li>
        <li><a href="#2通过控制器方法的形参获取请求参数">2、通过控制器方法的形参获取请求参数</a></li>
        <li><a href="#3requestparam">3、@RequestParam</a></li>
        <li><a href="#4requestheader">4、@RequestHeader</a></li>
        <li><a href="#5cookievalue">5、@CookieValue</a></li>
        <li><a href="#6通过pojo获取请求参数">6、通过POJO获取请求参数</a></li>
        <li><a href="#7解决获取请求参数的乱码问题">7、解决获取请求参数的乱码问题</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1使用servletapi向request域对象共享数据">1、使用ServletAPI向request域对象共享数据</a></li>
        <li><a href="#2使用modelandview向request域对象共享数据">2、使用ModelAndView向request域对象共享数据</a></li>
        <li><a href="#3使用model向request域对象共享数据">3、使用Model向request域对象共享数据</a></li>
        <li><a href="#4使用map向request域对象共享数据">4、使用map向request域对象共享数据</a></li>
        <li><a href="#5使用modelmap向request域对象共享数据">5、使用ModelMap向request域对象共享数据</a></li>
        <li><a href="#6modelmodelmapmap的关系">6、Model、ModelMap、Map的关系</a></li>
        <li><a href="#7向session域共享数据">7、向session域共享数据</a></li>
        <li><a href="#8向application域共享数据">8、向application域共享数据</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1thymeleafview">1、ThymeleafView</a></li>
        <li><a href="#2转发视图">2、转发视图</a></li>
        <li><a href="#3重定向视图">3、重定向视图</a></li>
        <li><a href="#4视图控制器view-controller">4、视图控制器view-controller</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1restful简介">1、RESTful简介</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a资源">a&gt;资源</a></li>
                <li><a href="#b资源的表述">b&gt;资源的表述</a></li>
                <li><a href="#c状态转移">c&gt;状态转移</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#2restful的实现">2、RESTful的实现</a></li>
        <li><a href="#3hiddenhttpmethodfilter">3、HiddenHttpMethodFilter</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1准备工作">1、准备工作</a></li>
        <li><a href="#2功能清单">2、功能清单</a></li>
        <li><a href="#3具体功能访问首页">3、具体功能：访问首页</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a配置view-controller">a&gt;配置view-controller</a></li>
                <li><a href="#b创建页面">b&gt;创建页面</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#4具体功能查询所有员工数据">4、具体功能：查询所有员工数据</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a控制器方法">a&gt;控制器方法</a></li>
                <li><a href="#b创建employee_listhtml">b&gt;创建employee_list.html</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#5具体功能删除">5、具体功能：删除</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a创建处理delete请求方式的表单">a&gt;创建处理delete请求方式的表单</a></li>
                <li><a href="#b删除超链接绑定点击事件">b&gt;删除超链接绑定点击事件</a></li>
                <li><a href="#c控制器方法">c&gt;控制器方法</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#6具体功能跳转到添加数据页面">6、具体功能：跳转到添加数据页面</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a配置view-controller-1">a&gt;配置view-controller</a></li>
                <li><a href="#b创建employee_addhtml">b&gt;创建employee_add.html</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#7具体功能执行保存">7、具体功能：执行保存</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a控制器方法-1">a&gt;控制器方法</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#8具体功能跳转到更新数据页面">8、具体功能：跳转到更新数据页面</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a修改超链接">a&gt;修改超链接</a></li>
                <li><a href="#b控制器方法">b&gt;控制器方法</a></li>
                <li><a href="#c创建employee_updatehtml">c&gt;创建employee_update.html</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#9具体功能执行更新">9、具体功能：执行更新</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a控制器方法-2">a&gt;控制器方法</a></li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1requestbody">1、@RequestBody</a></li>
        <li><a href="#2requestentity">2、RequestEntity</a></li>
        <li><a href="#3responsebody">3、@ResponseBody</a></li>
        <li><a href="#4springmvc处理json">4、SpringMVC处理json</a></li>
        <li><a href="#5springmvc处理ajax">5、SpringMVC处理ajax</a></li>
        <li><a href="#6restcontroller注解">6、@RestController注解</a></li>
        <li><a href="#7responseentity">7、ResponseEntity</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1文件下载">1、文件下载</a></li>
        <li><a href="#2文件上传">2、文件上传</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1拦截器的配置">1、拦截器的配置</a></li>
        <li><a href="#2拦截器的三个抽象方法">2、拦截器的三个抽象方法</a></li>
        <li><a href="#3多个拦截器的执行顺序">3、多个拦截器的执行顺序</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1基于配置的异常处理">1、基于配置的异常处理</a></li>
        <li><a href="#2基于注解的异常处理">2、基于注解的异常处理</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1创建初始化类代替webxml">1、创建初始化类，代替web.xml</a></li>
        <li><a href="#2创建springconfig配置类代替spring的配置文件">2、创建SpringConfig配置类，代替spring的配置文件</a></li>
        <li><a href="#3创建webconfig配置类代替springmvc的配置文件">3、创建WebConfig配置类，代替SpringMVC的配置文件</a></li>
        <li><a href="#4测试功能">4、测试功能</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#1springmvc常用组件">1、SpringMVC常用组件</a></li>
        <li><a href="#2dispatcherservlet初始化过程">2、DispatcherServlet初始化过程</a>
          <ul>
            <li>
              <ul>
                <li><a href="#a初始化webapplicationcontext">a&gt;初始化WebApplicationContext</a></li>
                <li><a href="#b创建webapplicationcontext">b&gt;创建WebApplicationContext</a></li>
                <li><a href="#cdispatcherservlet初始化策略">c&gt;DispatcherServlet初始化策略</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#3dispatcherservlet调用组件处理请求">3、DispatcherServlet调用组件处理请求</a>
          <ul>
            <li>
              <ul>
                <li><a href="#aprocessrequest">a&gt;processRequest()</a></li>
                <li><a href="#bdoservice">b&gt;doService()</a></li>
                <li><a href="#cdodispatch">c&gt;doDispatch()</a></li>
                <li><a href="#dprocessdispatchresult">d&gt;processDispatchResult()</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#4springmvc的执行流程">4、SpringMVC的执行流程</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><p>[TOC]</p>
<h1 id="一springmvc简介">一、SpringMVC简介</h1>
<h3 id="1什么是mvc">1、什么是MVC</h3>
<p>MVC是一种软件架构的思想，将软件按照模型、视图、控制器来划分</p>
<p>M：Model，模型层，指工程中的JavaBean，作用是处理数据</p>
<p>JavaBean分为两类：</p>
<ul>
<li>一类称为实体类Bean：专门存储业务数据的，如 Student、User 等</li>
<li>一类称为业务处理 Bean：指 Service 或 Dao 对象，专门用于处理业务逻辑和数据访问。</li>
</ul>
<p>V：View，视图层，指工程中的html或jsp等页面，作用是与用户进行交互，展示数据</p>
<p>C：Controller，控制层，指工程中的servlet，作用是接收请求和响应浏览器</p>
<p>MVC的工作流程：
用户通过视图层发送请求到服务器，在服务器中请求被Controller接收，Controller调用相应的Model层处理请求，处理完毕将结果返回到Controller，Controller再根据请求处理的结果找到相应的View视图，渲染数据后最终响应给浏览器</p>
<h3 id="2什么是springmvc">2、什么是SpringMVC</h3>
<p>SpringMVC是Spring的一个后续产品，是Spring的一个子项目</p>
<p>SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后，目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的<strong>首选方案</strong>。</p>
<blockquote>
<p>注：三层架构分为表述层（或表示层）、业务逻辑层、数据访问层，表述层表示前台页面和后台servlet</p>
</blockquote>
<h3 id="3springmvc的特点">3、SpringMVC的特点</h3>
<ul>
<li><strong>Spring 家族原生产品</strong>，与 IOC 容器等基础设施无缝对接</li>
<li><strong>基于原生的Servlet</strong>，通过了功能强大的<strong>前端控制器DispatcherServlet</strong>，对请求和响应进行统一处理</li>
<li>表述层各细分领域需要解决的问题<strong>全方位覆盖</strong>，提供<strong>全面解决方案</strong></li>
<li><strong>代码清新简洁</strong>，大幅度提升开发效率</li>
<li>内部组件化程度高，可插拔式组件<strong>即插即用</strong>，想要什么功能配置相应组件即可</li>
<li><strong>性能卓著</strong>，尤其适合现代大型、超大型互联网项目要求</li>
</ul>
<h1 id="二helloworld">二、HelloWorld</h1>
<h3 id="1开发环境">1、开发环境</h3>
<p>IDE：idea 2019.2</p>
<p>构建工具：maven3.5.4</p>
<p>服务器：tomcat7</p>
<p>Spring版本：5.3.1</p>
<h3 id="2创建maven工程">2、创建maven工程</h3>
<h5 id="a添加web模块">a&gt;添加web模块</h5>
<h5 id="b打包方式war">b&gt;打包方式：war</h5>
<h5 id="c引入依赖">c&gt;引入依赖</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependencies&gt;</span>
    <span class="c">&lt;!-- SpringMVC --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>org.springframework<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>spring-webmvc<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>5.3.1<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>

    <span class="c">&lt;!-- 日志 --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>ch.qos.logback<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>logback-classic<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>1.2.3<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>

    <span class="c">&lt;!-- ServletAPI --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>javax.servlet<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>javax.servlet-api<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>3.1.0<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;scope&gt;</span>provided<span class="nt">&lt;/scope&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>

    <span class="c">&lt;!-- Spring5和Thymeleaf整合包 --&gt;</span>
    <span class="nt">&lt;dependency&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>org.thymeleaf<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>thymeleaf-spring5<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>3.0.12.RELEASE<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;/dependency&gt;</span>
<span class="nt">&lt;/dependencies&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>注：由于 Maven 的传递性，我们不必将所有需要的包全部配置依赖，而是配置最顶端的依赖，其他靠传递性导入。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img%5cimg001.png"
        data-srcset="img%5cimg001.png, img%5cimg001.png 1.5x, img%5cimg001.png 2x"
        data-sizes="auto"
        alt="img\img001.png"
        title="images" /></p>
<h3 id="3配置webxml">3、配置web.xml</h3>
<p>注册SpringMVC的前端控制器DispatcherServlet</p>
<h5 id="a默认配置方式">a&gt;默认配置方式</h5>
<p>此配置作用下，SpringMVC的配置文件默认位于WEB-INF下，默认名称为&lt;servlet-name&gt;-servlet.xml，例如，以下配置所对应SpringMVC的配置文件位于WEB-INF下，文件名为springMVC-servlet.xml</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- 配置SpringMVC的前端控制器，对浏览器发送的请求统一进行处理 --&gt;</span>
<span class="nt">&lt;servlet&gt;</span>
    <span class="nt">&lt;servlet-name&gt;</span>springMVC<span class="nt">&lt;/servlet-name&gt;</span>
    <span class="nt">&lt;servlet-class&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="nt">&lt;/servlet-class&gt;</span>
<span class="nt">&lt;/servlet&gt;</span>
<span class="nt">&lt;servlet-mapping&gt;</span>
    <span class="nt">&lt;servlet-name&gt;</span>springMVC<span class="nt">&lt;/servlet-name&gt;</span>
    <span class="c">&lt;!--
</span><span class="c">        设置springMVC的核心控制器所能处理的请求的请求路径
</span><span class="c">        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
</span><span class="c">        但是/不能匹配.jsp请求路径的请求
</span><span class="c">    --&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/servlet-mapping&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b扩展配置方式">b&gt;扩展配置方式</h5>
<p>可通过init-param标签设置SpringMVC配置文件的位置和名称，通过load-on-startup标签设置SpringMVC前端控制器DispatcherServlet的初始化时间</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- 配置SpringMVC的前端控制器，对浏览器发送的请求统一进行处理 --&gt;</span>
<span class="nt">&lt;servlet&gt;</span>
    <span class="nt">&lt;servlet-name&gt;</span>springMVC<span class="nt">&lt;/servlet-name&gt;</span>
    <span class="nt">&lt;servlet-class&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="nt">&lt;/servlet-class&gt;</span>
    <span class="c">&lt;!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --&gt;</span>
    <span class="nt">&lt;init-param&gt;</span>
        <span class="c">&lt;!-- contextConfigLocation为固定值 --&gt;</span>
        <span class="nt">&lt;param-name&gt;</span>contextConfigLocation<span class="nt">&lt;/param-name&gt;</span>
        <span class="c">&lt;!-- 使用classpath:表示从类路径查找配置文件，例如maven工程中的src/main/resources --&gt;</span>
        <span class="nt">&lt;param-value&gt;</span>classpath:springMVC.xml<span class="nt">&lt;/param-value&gt;</span>
    <span class="nt">&lt;/init-param&gt;</span>
    <span class="c">&lt;!-- 
</span><span class="c"> 		作为框架的核心组件，在启动过程中有大量的初始化操作要做
</span><span class="c">		而这些操作放在第一次请求时才执行会严重影响访问速度
</span><span class="c">		因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
</span><span class="c">	--&gt;</span>
    <span class="nt">&lt;load-on-startup&gt;</span>1<span class="nt">&lt;/load-on-startup&gt;</span>
<span class="nt">&lt;/servlet&gt;</span>
<span class="nt">&lt;servlet-mapping&gt;</span>
    <span class="nt">&lt;servlet-name&gt;</span>springMVC<span class="nt">&lt;/servlet-name&gt;</span>
    <span class="c">&lt;!--
</span><span class="c">        设置springMVC的核心控制器所能处理的请求的请求路径
</span><span class="c">        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
</span><span class="c">        但是/不能匹配.jsp请求路径的请求
</span><span class="c">    --&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/servlet-mapping&gt;</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>&lt;url-pattern&gt;标签中使用/和/*的区别：</p>
<p>/所匹配的请求可以是/login或.html或.js或.css方式的请求路径，但是/不能匹配.jsp请求路径的请求</p>
<p>因此就可以避免在访问jsp页面时，该请求被DispatcherServlet处理，从而找不到相应的页面</p>
<p>/*则能够匹配所有请求，例如在使用过滤器时，若需要对所有请求进行过滤，就需要使用/*的写法</p>
</blockquote>
<h3 id="4创建请求控制器">4、创建请求控制器</h3>
<p>由于前端控制器对浏览器发送的请求进行了统一的处理，但是具体的请求有不同的处理过程，因此需要创建处理具体请求的类，即请求控制器</p>
<p>请求控制器中每一个处理请求的方法成为控制器方法</p>
<p>因为SpringMVC的控制器由一个POJO（普通的Java类）担任，因此需要通过@Controller注解将其标识为一个控制层组件，交给Spring的IoC容器管理，此时SpringMVC才能够识别控制器的存在</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Controller</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloController</span> <span class="o">{</span>
    
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5创建springmvc的配置文件">5、创建springMVC的配置文件</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- 自动扫描包 --&gt;</span>
<span class="nt">&lt;context:component-scan</span> <span class="na">base-package=</span><span class="s">&#34;com.atguigu.mvc.controller&#34;</span><span class="nt">/&gt;</span>

<span class="c">&lt;!-- 配置Thymeleaf视图解析器 --&gt;</span>
<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&#34;viewResolver&#34;</span> <span class="na">class=</span><span class="s">&#34;org.thymeleaf.spring5.view.ThymeleafViewResolver&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;order&#34;</span> <span class="na">value=</span><span class="s">&#34;1&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;characterEncoding&#34;</span> <span class="na">value=</span><span class="s">&#34;UTF-8&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;templateEngine&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&#34;org.thymeleaf.spring5.SpringTemplateEngine&#34;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;templateResolver&#34;</span><span class="nt">&gt;</span>
                <span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&#34;org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver&#34;</span><span class="nt">&gt;</span>
    
                    <span class="c">&lt;!-- 视图前缀 --&gt;</span>
                    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;prefix&#34;</span> <span class="na">value=</span><span class="s">&#34;/WEB-INF/templates/&#34;</span><span class="nt">/&gt;</span>
    
                    <span class="c">&lt;!-- 视图后缀 --&gt;</span>
                    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;suffix&#34;</span> <span class="na">value=</span><span class="s">&#34;.html&#34;</span><span class="nt">/&gt;</span>
                    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;templateMode&#34;</span> <span class="na">value=</span><span class="s">&#34;HTML5&#34;</span><span class="nt">/&gt;</span>
                    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;characterEncoding&#34;</span> <span class="na">value=</span><span class="s">&#34;UTF-8&#34;</span> <span class="nt">/&gt;</span>
                <span class="nt">&lt;/bean&gt;</span>
            <span class="nt">&lt;/property&gt;</span>
        <span class="nt">&lt;/bean&gt;</span>
    <span class="nt">&lt;/property&gt;</span>
<span class="nt">&lt;/bean&gt;</span>

<span class="c">&lt;!-- 
</span><span class="c">   处理静态资源，例如html、js、css、jpg
</span><span class="c">  若只设置该标签，则只能访问静态资源，其他请求则无法访问
</span><span class="c">  此时必须设置&lt;mvc:annotation-driven/&gt;解决问题
</span><span class="c"> --&gt;</span>
<span class="nt">&lt;mvc:default-servlet-handler/&gt;</span>

<span class="c">&lt;!-- 开启mvc注解驱动 --&gt;</span>
<span class="nt">&lt;mvc:annotation-driven&gt;</span>
    <span class="nt">&lt;mvc:message-converters&gt;</span>
        <span class="c">&lt;!-- 处理响应中文内容乱码 --&gt;</span>
        <span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&#34;org.springframework.http.converter.StringHttpMessageConverter&#34;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;defaultCharset&#34;</span> <span class="na">value=</span><span class="s">&#34;UTF-8&#34;</span> <span class="nt">/&gt;</span>
            <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;supportedMediaTypes&#34;</span><span class="nt">&gt;</span>
                <span class="nt">&lt;list&gt;</span>
                    <span class="nt">&lt;value&gt;</span>text/html<span class="nt">&lt;/value&gt;</span>
                    <span class="nt">&lt;value&gt;</span>application/json<span class="nt">&lt;/value&gt;</span>
                <span class="nt">&lt;/list&gt;</span>
            <span class="nt">&lt;/property&gt;</span>
        <span class="nt">&lt;/bean&gt;</span>
    <span class="nt">&lt;/mvc:message-converters&gt;</span>
<span class="nt">&lt;/mvc:annotation-driven&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6测试helloworld">6、测试HelloWorld</h3>
<h5 id="a实现对首页的访问">a&gt;实现对首页的访问</h5>
<p>在请求控制器中创建处理请求的方法</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="c1">// @RequestMapping注解：处理请求和控制器方法之间的映射关系
</span><span class="c1">// @RequestMapping注解的value属性可以通过请求地址匹配请求，/表示的当前工程的上下文路径
</span><span class="c1">// localhost:8080/springMVC/
</span><span class="c1"></span><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">index</span><span class="o">()</span> <span class="o">{</span>
    <span class="c1">//设置视图名称
</span><span class="c1"></span>    <span class="k">return</span> <span class="s">&#34;index&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b通过超链接跳转到指定页面">b&gt;通过超链接跳转到指定页面</h5>
<p>在主页index.html中设置超链接</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;en&#34;</span> <span class="na">xmlns:th</span><span class="o">=</span><span class="s">&#34;http://www.thymeleaf.org&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&#34;UTF-8&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>首页<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>首页<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/hello}&#34;</span><span class="p">&gt;</span>HelloWorld<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">/&gt;</span>
<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>在请求控制器中创建处理请求的方法</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/hello&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">HelloWorld</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="s">&#34;target&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="7总结">7、总结</h3>
<p>浏览器发送请求，若请求地址符合前端控制器的url-pattern，该请求就会被前端控制器DispatcherServlet处理。前端控制器会读取SpringMVC的核心配置文件，通过扫描组件找到控制器，将请求地址和控制器中@RequestMapping注解的value属性值进行匹配，若匹配成功，该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称，该视图名称会被视图解析器解析，加上前缀和后缀组成视图的路径，通过Thymeleaf对视图进行渲染，最终转发到视图所对应页面</p>
<h1 id="三requestmapping注解">三、@RequestMapping注解</h1>
<h3 id="1requestmapping注解的功能">1、@RequestMapping注解的功能</h3>
<p>从注解名称上我们可以看到，@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来，建立映射关系。</p>
<p>SpringMVC 接收到指定的请求，就会来找到在映射关系中对应的控制器方法来处理这个请求。</p>
<h3 id="2requestmapping注解的位置">2、@RequestMapping注解的位置</h3>
<p>@RequestMapping标识一个类：设置映射请求的请求路径的初始信息</p>
<p>@RequestMapping标识一个方法：设置映射请求请求路径的具体信息</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Controller</span>
<span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/test&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">RequestMappingController</span> <span class="o">{</span>

	<span class="c1">//此时请求映射所映射的请求的请求路径为：/test/testRequestMapping
</span><span class="c1"></span>    <span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testRequestMapping&#34;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestMapping</span><span class="o">(){</span>
        <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3requestmapping注解的value属性">3、@RequestMapping注解的value属性</h3>
<p>@RequestMapping注解的value属性通过请求的请求地址匹配请求映射</p>
<p>@RequestMapping注解的value属性是一个字符串类型的数组，表示该请求映射能够匹配多个请求地址所对应的请求</p>
<p>@RequestMapping注解的value属性必须设置，至少通过请求地址匹配请求映射</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/testRequestMapping}&#34;</span><span class="p">&gt;</span>测试@RequestMapping的value属性--&gt;/testRequestMapping<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/test}&#34;</span><span class="p">&gt;</span>测试@RequestMapping的value属性--&gt;/test<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span>
        <span class="n">value</span> <span class="o">=</span> <span class="o">{</span><span class="s">&#34;/testRequestMapping&#34;</span><span class="o">,</span> <span class="s">&#34;/test&#34;</span><span class="o">}</span>
<span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestMapping</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4requestmapping注解的method属性">4、@RequestMapping注解的method属性</h3>
<p>@RequestMapping注解的method属性通过请求的请求方式（get或post）匹配请求映射</p>
<p>@RequestMapping注解的method属性是一个RequestMethod类型的数组，表示该请求映射能够匹配多种请求方式的请求</p>
<p>若当前请求的请求地址满足请求映射的value属性，但是请求方式不满足method属性，则浏览器报错405：Request method &lsquo;POST&rsquo; not supported</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/test}&#34;</span><span class="p">&gt;</span>测试@RequestMapping的value属性--&gt;/test<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">form</span> <span class="na">th:action</span><span class="o">=</span><span class="s">&#34;@{/test}&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;submit&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span>
        <span class="n">value</span> <span class="o">=</span> <span class="o">{</span><span class="s">&#34;/testRequestMapping&#34;</span><span class="o">,</span> <span class="s">&#34;/test&#34;</span><span class="o">},</span>
        <span class="n">method</span> <span class="o">=</span> <span class="o">{</span><span class="n">RequestMethod</span><span class="o">.</span><span class="na">GET</span><span class="o">,</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">POST</span><span class="o">}</span>
<span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestMapping</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>1、对于处理指定请求方式的控制器方法，SpringMVC中提供了@RequestMapping的派生注解</p>
<p>处理get请求的映射&ndash;&gt;@GetMapping</p>
<p>处理post请求的映射&ndash;&gt;@PostMapping</p>
<p>处理put请求的映射&ndash;&gt;@PutMapping</p>
<p>处理delete请求的映射&ndash;&gt;@DeleteMapping</p>
<p>2、常用的请求方式有get，post，put，delete</p>
<p>但是目前浏览器只支持get和post，若在form表单提交时，为method设置了其他请求方式的字符串（put或delete），则按照默认的请求方式get处理</p>
<p>若要发送put和delete请求，则需要通过spring提供的过滤器HiddenHttpMethodFilter，在RESTful部分会讲到</p>
</blockquote>
<h3 id="5requestmapping注解的params属性了解">5、@RequestMapping注解的params属性（了解）</h3>
<p>@RequestMapping注解的params属性通过请求的请求参数匹配请求映射</p>
<p>@RequestMapping注解的params属性是一个字符串类型的数组，可以通过四种表达式设置请求参数和请求映射的匹配关系</p>
<p>&ldquo;param&rdquo;：要求请求映射所匹配的请求必须携带param请求参数</p>
<p>&ldquo;!param&rdquo;：要求请求映射所匹配的请求必须不能携带param请求参数</p>
<p>&ldquo;param=value&rdquo;：要求请求映射所匹配的请求必须携带param请求参数且param=value</p>
<p>&ldquo;param!=value&rdquo;：要求请求映射所匹配的请求必须携带param请求参数但是param!=value</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/test(username=&#39;admin&#39;,password=123456)&#34;</span><span class="p">&gt;</span>测试@RequestMapping的params属性--&gt;/test<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span>
        <span class="n">value</span> <span class="o">=</span> <span class="o">{</span><span class="s">&#34;/testRequestMapping&#34;</span><span class="o">,</span> <span class="s">&#34;/test&#34;</span><span class="o">}</span>
        <span class="o">,</span><span class="n">method</span> <span class="o">=</span> <span class="o">{</span><span class="n">RequestMethod</span><span class="o">.</span><span class="na">GET</span><span class="o">,</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">POST</span><span class="o">}</span>
        <span class="o">,</span><span class="n">params</span> <span class="o">=</span> <span class="o">{</span><span class="s">&#34;username&#34;</span><span class="o">,</span><span class="s">&#34;password!=123456&#34;</span><span class="o">}</span>
<span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestMapping</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>若当前请求满足@RequestMapping注解的value和method属性，但是不满足params属性，此时页面回报错400：Parameter conditions &ldquo;username, password!=123456&rdquo; not met for actual request parameters: username={admin}, password={123456}</p>
</blockquote>
<h3 id="6requestmapping注解的headers属性了解">6、@RequestMapping注解的headers属性（了解）</h3>
<p>@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射</p>
<p>@RequestMapping注解的headers属性是一个字符串类型的数组，可以通过四种表达式设置请求头信息和请求映射的匹配关系</p>
<p>&ldquo;header&rdquo;：要求请求映射所匹配的请求必须携带header请求头信息</p>
<p>&ldquo;!header&rdquo;：要求请求映射所匹配的请求必须不能携带header请求头信息</p>
<p>&ldquo;header=value&rdquo;：要求请求映射所匹配的请求必须携带header请求头信息且header=value</p>
<p>&ldquo;header!=value&rdquo;：要求请求映射所匹配的请求必须携带header请求头信息且header!=value</p>
<p>若当前请求满足@RequestMapping注解的value和method属性，但是不满足headers属性，此时页面显示404错误，即资源未找到</p>
<h3 id="7springmvc支持ant风格的路径">7、SpringMVC支持ant风格的路径</h3>
<p>？：表示任意的单个字符</p>
<p>*：表示任意的0个或多个字符</p>
<p>**：表示任意的一层或多层目录</p>
<p>注意：在使用**时，只能使用/**/xxx的方式</p>
<h3 id="8springmvc支持路径中的占位符重点">8、SpringMVC支持路径中的占位符（重点）</h3>
<p>原始方式：/deleteUser?id=1</p>
<p>rest方式：/deleteUser/1</p>
<p>SpringMVC路径中的占位符常用于RESTful风格中，当请求路径中将某些数据通过路径的方式传输到服务器中，就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据，在通过@PathVariable注解，将占位符所表示的数据赋值给控制器方法的形参</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/testRest/1/admin}&#34;</span><span class="p">&gt;</span>测试路径中的占位符--&gt;/testRest<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testRest/{id}/{username}&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRest</span><span class="o">(</span><span class="nd">@PathVariable</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">)</span> <span class="n">String</span> <span class="n">id</span><span class="o">,</span> <span class="nd">@PathVariable</span><span class="o">(</span><span class="s">&#34;username&#34;</span><span class="o">)</span> <span class="n">String</span> <span class="n">username</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;id:&#34;</span><span class="o">+</span><span class="n">id</span><span class="o">+</span><span class="s">&#34;,username:&#34;</span><span class="o">+</span><span class="n">username</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">//最终输出的内容为--&gt;id:1,username:admin
</span></code></pre></td></tr></table>
</div>
</div><h1 id="四springmvc获取请求参数">四、SpringMVC获取请求参数</h1>
<h3 id="1通过servletapi获取">1、通过ServletAPI获取</h3>
<p>将HttpServletRequest作为控制器方法的形参，此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testParam&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testParam</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">){</span>
    <span class="n">String</span> <span class="n">username</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="na">getParameter</span><span class="o">(</span><span class="s">&#34;username&#34;</span><span class="o">);</span>
    <span class="n">String</span> <span class="n">password</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="na">getParameter</span><span class="o">(</span><span class="s">&#34;password&#34;</span><span class="o">);</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;username:&#34;</span><span class="o">+</span><span class="n">username</span><span class="o">+</span><span class="s">&#34;,password:&#34;</span><span class="o">+</span><span class="n">password</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2通过控制器方法的形参获取请求参数">2、通过控制器方法的形参获取请求参数</h3>
<p>在控制器方法的形参位置，设置和请求参数同名的形参，当浏览器发送请求，匹配到请求映射时，在DispatcherServlet中就会将请求参数赋值给相应的形参</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/testParam(username=&#39;admin&#39;,password=123456)}&#34;</span><span class="p">&gt;</span>测试获取请求参数--&gt;/testParam<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testParam&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testParam</span><span class="o">(</span><span class="n">String</span> <span class="n">username</span><span class="o">,</span> <span class="n">String</span> <span class="n">password</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;username:&#34;</span><span class="o">+</span><span class="n">username</span><span class="o">+</span><span class="s">&#34;,password:&#34;</span><span class="o">+</span><span class="n">password</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>若请求所传输的请求参数中有多个同名的请求参数，此时可以在控制器方法的形参中设置字符串数组或者字符串类型的形参接收此请求参数</p>
<p>若使用字符串数组类型的形参，此参数的数组中包含了每一个数据</p>
<p>若使用字符串类型的形参，此参数的值为每个数据中间使用逗号拼接的结果</p>
</blockquote>
<h3 id="3requestparam">3、@RequestParam</h3>
<p>@RequestParam是将请求参数和控制器方法的形参创建映射关系</p>
<p>@RequestParam注解一共有三个属性：</p>
<p>value：指定为形参赋值的请求参数的参数名</p>
<p>required：设置是否必须传输此请求参数，默认值为true</p>
<p>若设置为true时，则当前请求必须传输value所指定的请求参数，若没有传输该请求参数，且没有设置defaultValue属性，则页面报错400：Required String parameter &lsquo;xxx&rsquo; is not present；若设置为false，则当前请求不是必须传输value所指定的请求参数，若没有传输，则注解所标识的形参的值为null</p>
<p>defaultValue：不管required属性值为true或false，当value所指定的请求参数没有传输或传输的值为&quot;&ldquo;时，则使用默认值为形参赋值</p>
<h3 id="4requestheader">4、@RequestHeader</h3>
<p>@RequestHeader是将请求头信息和控制器方法的形参创建映射关系</p>
<p>@RequestHeader注解一共有三个属性：value、required、defaultValue，用法同@RequestParam</p>
<h3 id="5cookievalue">5、@CookieValue</h3>
<p>@CookieValue是将cookie数据和控制器方法的形参创建映射关系</p>
<p>@CookieValue注解一共有三个属性：value、required、defaultValue，用法同@RequestParam</p>
<h3 id="6通过pojo获取请求参数">6、通过POJO获取请求参数</h3>
<p>可以在控制器方法的形参位置设置一个实体类类型的形参，此时若浏览器传输的请求参数的参数名和实体类中的属性名一致，那么请求参数就会为此属性赋值</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">form</span> <span class="na">th:action</span><span class="o">=</span><span class="s">&#34;@{/testpojo}&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    用户名：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;username&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    密码：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;password&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;password&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    性别：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;sex&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;男&#34;</span><span class="p">&gt;</span>男<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;sex&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;女&#34;</span><span class="p">&gt;</span>女<span class="p">&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    年龄：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;age&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    邮箱：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;email&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;submit&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testpojo&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testPOJO</span><span class="o">(</span><span class="n">User</span> <span class="n">user</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">//最终结果--&gt;User{id=null, username=&#39;张三&#39;, password=&#39;123&#39;, age=23, sex=&#39;男&#39;, email=&#39;123@qq.com&#39;}
</span></code></pre></td></tr></table>
</div>
</div><h3 id="7解决获取请求参数的乱码问题">7、解决获取请求参数的乱码问题</h3>
<p>解决获取请求参数的乱码问题，可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter，但是必须在web.xml中进行注册</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--配置springMVC的编码过滤器--&gt;</span>
<span class="nt">&lt;filter&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>CharacterEncodingFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;filter-class&gt;</span>org.springframework.web.filter.CharacterEncodingFilter<span class="nt">&lt;/filter-class&gt;</span>
    <span class="nt">&lt;init-param&gt;</span>
        <span class="nt">&lt;param-name&gt;</span>encoding<span class="nt">&lt;/param-name&gt;</span>
        <span class="nt">&lt;param-value&gt;</span>UTF-8<span class="nt">&lt;/param-value&gt;</span>
    <span class="nt">&lt;/init-param&gt;</span>
    <span class="nt">&lt;init-param&gt;</span>
        <span class="nt">&lt;param-name&gt;</span>forceResponseEncoding<span class="nt">&lt;/param-name&gt;</span>
        <span class="nt">&lt;param-value&gt;</span>true<span class="nt">&lt;/param-value&gt;</span>
    <span class="nt">&lt;/init-param&gt;</span>
<span class="nt">&lt;/filter&gt;</span>
<span class="nt">&lt;filter-mapping&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>CharacterEncodingFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/*<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/filter-mapping&gt;</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>SpringMVC中处理编码的过滤器一定要配置到其他过滤器之前，否则无效</p>
</blockquote>
<h1 id="五域对象共享数据">五、域对象共享数据</h1>
<h3 id="1使用servletapi向request域对象共享数据">1、使用ServletAPI向request域对象共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testServletAPI&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testServletAPI</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">){</span>
    <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="s">&#34;testScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,servletAPI&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2使用modelandview向request域对象共享数据">2、使用ModelAndView向request域对象共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testModelAndView&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">ModelAndView</span> <span class="nf">testModelAndView</span><span class="o">(){</span>
    <span class="cm">/**
</span><span class="cm">     * ModelAndView有Model和View的功能
</span><span class="cm">     * Model主要用于向请求域共享数据
</span><span class="cm">     * View主要用于设置视图，实现页面跳转
</span><span class="cm">     */</span>
    <span class="n">ModelAndView</span> <span class="n">mav</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ModelAndView</span><span class="o">();</span>
    <span class="c1">//向请求域共享数据
</span><span class="c1"></span>    <span class="n">mav</span><span class="o">.</span><span class="na">addObject</span><span class="o">(</span><span class="s">&#34;testScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,ModelAndView&#34;</span><span class="o">);</span>
    <span class="c1">//设置视图，实现页面跳转
</span><span class="c1"></span>    <span class="n">mav</span><span class="o">.</span><span class="na">setViewName</span><span class="o">(</span><span class="s">&#34;success&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="n">mav</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3使用model向request域对象共享数据">3、使用Model向request域对象共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testModel&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testModel</span><span class="o">(</span><span class="n">Model</span> <span class="n">model</span><span class="o">){</span>
    <span class="n">model</span><span class="o">.</span><span class="na">addAttribute</span><span class="o">(</span><span class="s">&#34;testScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,Model&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4使用map向request域对象共享数据">4、使用map向request域对象共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testMap&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testMap</span><span class="o">(</span><span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span> <span class="n">map</span><span class="o">){</span>
    <span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&#34;testScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,Map&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5使用modelmap向request域对象共享数据">5、使用ModelMap向request域对象共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testModelMap&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testModelMap</span><span class="o">(</span><span class="n">ModelMap</span> <span class="n">modelMap</span><span class="o">){</span>
    <span class="n">modelMap</span><span class="o">.</span><span class="na">addAttribute</span><span class="o">(</span><span class="s">&#34;testScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,ModelMap&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6modelmodelmapmap的关系">6、Model、ModelMap、Map的关系</h3>
<p>Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">public interface Model{}
public class ModelMap extends LinkedHashMap&lt;String, Object&gt; {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
</code></pre></td></tr></table>
</div>
</div><h3 id="7向session域共享数据">7、向session域共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testSession&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testSession</span><span class="o">(</span><span class="n">HttpSession</span> <span class="n">session</span><span class="o">){</span>
    <span class="n">session</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="s">&#34;testSessionScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,session&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="8向application域共享数据">8、向application域共享数据</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testApplication&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testApplication</span><span class="o">(</span><span class="n">HttpSession</span> <span class="n">session</span><span class="o">){</span>
	<span class="n">ServletContext</span> <span class="n">application</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="na">getServletContext</span><span class="o">();</span>
    <span class="n">application</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="s">&#34;testApplicationScope&#34;</span><span class="o">,</span> <span class="s">&#34;hello,application&#34;</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="六springmvc的视图">六、SpringMVC的视图</h1>
<p>SpringMVC中的视图是View接口，视图的作用渲染数据，将模型Model中的数据展示给用户</p>
<p>SpringMVC视图的种类很多，默认有转发视图和重定向视图</p>
<p>当工程引入jstl的依赖，转发视图会自动转换为JstlView</p>
<p>若使用的视图技术为Thymeleaf，在SpringMVC的配置文件中配置了Thymeleaf的视图解析器，由此视图解析器解析之后所得到的是ThymeleafView</p>
<h3 id="1thymeleafview">1、ThymeleafView</h3>
<p>当控制器方法中所设置的视图名称没有任何前缀时，此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析，视图名称拼接视图前缀和视图后缀所得到的最终路径，会通过转发的方式实现跳转</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testHello&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testHello</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;hello&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img002.png"
        data-srcset="img/img002.png, img/img002.png 1.5x, img/img002.png 2x"
        data-sizes="auto"
        alt="img/img002.png"
        title="img/img002.png" /></p>
<h3 id="2转发视图">2、转发视图</h3>
<p>SpringMVC中默认的转发视图是InternalResourceView</p>
<p>SpringMVC中创建转发视图的情况：</p>
<p>当控制器方法中所设置的视图名称以&quot;forward:&ldquo;为前缀时，创建InternalResourceView视图，此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析，而是会将前缀&quot;forward:&ldquo;去掉，剩余部分作为最终路径通过转发的方式实现跳转</p>
<p>例如&quot;forward:/&quot;，&ldquo;forward:/employee&rdquo;</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testForward&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testForward</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;forward:/testHello&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img003.png"
        data-srcset="img/img003.png, img/img003.png 1.5x, img/img003.png 2x"
        data-sizes="auto"
        alt="img/img003.png"
        title="image-20210706201316593" /></p>
<h3 id="3重定向视图">3、重定向视图</h3>
<p>SpringMVC中默认的重定向视图是RedirectView</p>
<p>当控制器方法中所设置的视图名称以&quot;redirect:&ldquo;为前缀时，创建RedirectView视图，此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析，而是会将前缀&quot;redirect:&ldquo;去掉，剩余部分作为最终路径通过重定向的方式实现跳转</p>
<p>例如&quot;redirect:/&quot;，&ldquo;redirect:/employee&rdquo;</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testRedirect&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRedirect</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;redirect:/testHello&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img004.png"
        data-srcset="img/img004.png, img/img004.png 1.5x, img/img004.png 2x"
        data-sizes="auto"
        alt="img/img004.png"
        title="image-20210706201602267" /></p>
<blockquote>
<p>注：</p>
<p>重定向视图在解析时，会先将redirect:前缀去掉，然后会判断剩余部分是否以/开头，若是则会自动拼接上下文路径</p>
</blockquote>
<h3 id="4视图控制器view-controller">4、视图控制器view-controller</h3>
<p>当控制器方法中，仅仅用来实现页面跳转，即只需要设置视图名称时，可以将处理器方法使用view-controller标签进行表示</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--
</span><span class="c">	path：设置处理的请求地址
</span><span class="c">	view-name：设置请求地址所对应的视图名称
</span><span class="c">--&gt;</span>
<span class="nt">&lt;mvc:view-controller</span> <span class="na">path=</span><span class="s">&#34;/testView&#34;</span> <span class="na">view-name=</span><span class="s">&#34;success&#34;</span><span class="nt">&gt;&lt;/mvc:view-controller&gt;</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>当SpringMVC中设置任何一个view-controller时，其他控制器中的请求映射将全部失效，此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签：</p>
<p>&lt;mvc:annotation-driven /&gt;</p>
</blockquote>
<h1 id="七restful">七、RESTful</h1>
<h3 id="1restful简介">1、RESTful简介</h3>
<p>REST：<strong>Re</strong>presentational <strong>S</strong>tate <strong>T</strong>ransfer，表现层资源状态转移。</p>
<h5 id="a资源">a&gt;资源</h5>
<p>资源是一种看待服务器的方式，即，将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念，所以它不仅仅能代表服务器文件系统中的一个文件、数据库中的一张表等等具体的东西，可以将资源设计的要多抽象有多抽象，只要想象力允许而且客户端应用开发者能够理解。与面向对象设计类似，资源是以名词为核心来组织的，首先关注的是名词。一个资源可以由一个或多个URI来标识。URI既是资源的名称，也是资源在Web上的地址。对某个资源感兴趣的客户端应用，可以通过资源的URI与其进行交互。</p>
<h5 id="b资源的表述">b&gt;资源的表述</h5>
<p>资源的表述是一段对于资源在某个特定时刻的状态的描述。可以在客户端-服务器端之间转移（交换）。资源的表述可以有多种格式，例如HTML/XML/JSON/纯文本/图片/视频/音频等等。资源的表述格式可以通过协商机制来确定。请求-响应方向的表述通常使用不同的格式。</p>
<h5 id="c状态转移">c&gt;状态转移</h5>
<p>状态转移说的是：在客户端和服务器端之间转移（transfer）代表资源状态的表述。通过转移和操作资源的表述，来间接实现操作资源的目的。</p>
<h3 id="2restful的实现">2、RESTful的实现</h3>
<p>具体说，就是 HTTP 协议里面，四个表示操作方式的动词：GET、POST、PUT、DELETE。</p>
<p>它们分别对应四种基本操作：GET 用来获取资源，POST 用来新建资源，PUT 用来更新资源，DELETE 用来删除资源。</p>
<p>REST 风格提倡 URL 地址使用统一的风格设计，从前到后各个单词使用斜杠分开，不使用问号键值对方式携带请求参数，而是将要发送给服务器的数据作为 URL 地址的一部分，以保证整体风格的一致性。</p>
<table>
<thead>
<tr>
<th>操作</th>
<th>传统方式</th>
<th>REST风格</th>
</tr>
</thead>
<tbody>
<tr>
<td>查询操作</td>
<td>getUserById?id=1</td>
<td>user/1&ndash;&gt;get请求方式</td>
</tr>
<tr>
<td>保存操作</td>
<td>saveUser</td>
<td>user&ndash;&gt;post请求方式</td>
</tr>
<tr>
<td>删除操作</td>
<td>deleteUser?id=1</td>
<td>user/1&ndash;&gt;delete请求方式</td>
</tr>
<tr>
<td>更新操作</td>
<td>updateUser</td>
<td>user&ndash;&gt;put请求方式</td>
</tr>
</tbody>
</table>
<h3 id="3hiddenhttpmethodfilter">3、HiddenHttpMethodFilter</h3>
<p>由于浏览器只支持发送get和post方式的请求，那么该如何发送put和delete请求呢？</p>
<p>SpringMVC 提供了 <strong>HiddenHttpMethodFilter</strong> 帮助我们<strong>将 POST 请求转换为 DELETE 或 PUT 请求</strong></p>
<p><strong>HiddenHttpMethodFilter</strong> 处理put和delete请求的条件：</p>
<p>a&gt;当前请求的请求方式必须为post</p>
<p>b&gt;当前请求必须传输请求参数_method</p>
<p>满足以上条件，<strong>HiddenHttpMethodFilter</strong> 过滤器就会将当前请求的请求方式转换为请求参数_method的值，因此请求参数_method的值才是最终的请求方式</p>
<p>在web.xml中注册<strong>HiddenHttpMethodFilter</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;filter&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>HiddenHttpMethodFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;filter-class&gt;</span>org.springframework.web.filter.HiddenHttpMethodFilter<span class="nt">&lt;/filter-class&gt;</span>
<span class="nt">&lt;/filter&gt;</span>
<span class="nt">&lt;filter-mapping&gt;</span>
    <span class="nt">&lt;filter-name&gt;</span>HiddenHttpMethodFilter<span class="nt">&lt;/filter-name&gt;</span>
    <span class="nt">&lt;url-pattern&gt;</span>/*<span class="nt">&lt;/url-pattern&gt;</span>
<span class="nt">&lt;/filter-mapping&gt;</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：</p>
<p>目前为止，SpringMVC中提供了两个过滤器：CharacterEncodingFilter和HiddenHttpMethodFilter</p>
<p>在web.xml中注册时，必须先注册CharacterEncodingFilter，再注册HiddenHttpMethodFilter</p>
<p>原因：</p>
<ul>
<li>
<p>在 CharacterEncodingFilter 中通过 request.setCharacterEncoding(encoding) 方法设置字符集的</p>
</li>
<li>
<p>request.setCharacterEncoding(encoding) 方法要求前面不能有任何获取请求参数的操作</p>
</li>
<li>
<p>而 HiddenHttpMethodFilter 恰恰有一个获取请求方式的操作：</p>
</li>
<li>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">String paramValue = request.getParameter(this.methodParam);
</code></pre></td></tr></table>
</div>
</div></li>
</ul>
</blockquote>
<h1 id="八restful案例">八、RESTful案例</h1>
<h3 id="1准备工作">1、准备工作</h3>
<p>和传统 CRUD 一样，实现对员工信息的增删改查。</p>
<ul>
<li>
<p>搭建环境</p>
</li>
<li>
<p>准备实体类</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.atguigu.mvc.bean</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Employee</span> <span class="o">{</span>

   <span class="kd">private</span> <span class="n">Integer</span> <span class="n">id</span><span class="o">;</span>
   <span class="kd">private</span> <span class="n">String</span> <span class="n">lastName</span><span class="o">;</span>

   <span class="kd">private</span> <span class="n">String</span> <span class="n">email</span><span class="o">;</span>
   <span class="c1">//1 male, 0 female
</span><span class="c1"></span>   <span class="kd">private</span> <span class="n">Integer</span> <span class="n">gender</span><span class="o">;</span>

   <span class="kd">public</span> <span class="n">Integer</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">id</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="n">String</span> <span class="nf">getLastName</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">lastName</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setLastName</span><span class="o">(</span><span class="n">String</span> <span class="n">lastName</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="na">lastName</span> <span class="o">=</span> <span class="n">lastName</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="n">String</span> <span class="nf">getEmail</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">email</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setEmail</span><span class="o">(</span><span class="n">String</span> <span class="n">email</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="na">email</span> <span class="o">=</span> <span class="n">email</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="n">Integer</span> <span class="nf">getGender</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">gender</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setGender</span><span class="o">(</span><span class="n">Integer</span> <span class="n">gender</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="na">gender</span> <span class="o">=</span> <span class="n">gender</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="nf">Employee</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">,</span> <span class="n">String</span> <span class="n">lastName</span><span class="o">,</span> <span class="n">String</span> <span class="n">email</span><span class="o">,</span> <span class="n">Integer</span> <span class="n">gender</span><span class="o">)</span> <span class="o">{</span>
      <span class="kd">super</span><span class="o">();</span>
      <span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="na">lastName</span> <span class="o">=</span> <span class="n">lastName</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="na">email</span> <span class="o">=</span> <span class="n">email</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="na">gender</span> <span class="o">=</span> <span class="n">gender</span><span class="o">;</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="nf">Employee</span><span class="o">()</span> <span class="o">{</span>
   <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>准备dao模拟数据</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.atguigu.mvc.dao</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Collection</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.Map</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.atguigu.mvc.bean.Employee</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Repository</span><span class="o">;</span>


<span class="nd">@Repository</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">EmployeeDao</span> <span class="o">{</span>

   <span class="kd">private</span> <span class="kd">static</span> <span class="n">Map</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">,</span> <span class="n">Employee</span><span class="o">&gt;</span> <span class="n">employees</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

   <span class="kd">static</span><span class="o">{</span>
      <span class="n">employees</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">,</span> <span class="n">Employee</span><span class="o">&gt;();</span>

      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">1001</span><span class="o">,</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="n">1001</span><span class="o">,</span> <span class="s">&#34;E-AA&#34;</span><span class="o">,</span> <span class="s">&#34;aa@163.com&#34;</span><span class="o">,</span> <span class="n">1</span><span class="o">));</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">1002</span><span class="o">,</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="n">1002</span><span class="o">,</span> <span class="s">&#34;E-BB&#34;</span><span class="o">,</span> <span class="s">&#34;bb@163.com&#34;</span><span class="o">,</span> <span class="n">1</span><span class="o">));</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">1003</span><span class="o">,</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="n">1003</span><span class="o">,</span> <span class="s">&#34;E-CC&#34;</span><span class="o">,</span> <span class="s">&#34;cc@163.com&#34;</span><span class="o">,</span> <span class="n">0</span><span class="o">));</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">1004</span><span class="o">,</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="n">1004</span><span class="o">,</span> <span class="s">&#34;E-DD&#34;</span><span class="o">,</span> <span class="s">&#34;dd@163.com&#34;</span><span class="o">,</span> <span class="n">0</span><span class="o">));</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">1005</span><span class="o">,</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="n">1005</span><span class="o">,</span> <span class="s">&#34;E-EE&#34;</span><span class="o">,</span> <span class="s">&#34;ee@163.com&#34;</span><span class="o">,</span> <span class="n">1</span><span class="o">));</span>
   <span class="o">}</span>

   <span class="kd">private</span> <span class="kd">static</span> <span class="n">Integer</span> <span class="n">initId</span> <span class="o">=</span> <span class="n">1006</span><span class="o">;</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">save</span><span class="o">(</span><span class="n">Employee</span> <span class="n">employee</span><span class="o">){</span>
      <span class="k">if</span><span class="o">(</span><span class="n">employee</span><span class="o">.</span><span class="na">getId</span><span class="o">()</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
         <span class="n">employee</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="n">initId</span><span class="o">++);</span>
      <span class="o">}</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">employee</span><span class="o">.</span><span class="na">getId</span><span class="o">(),</span> <span class="n">employee</span><span class="o">);</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="n">Collection</span><span class="o">&lt;</span><span class="n">Employee</span><span class="o">&gt;</span> <span class="nf">getAll</span><span class="o">(){</span>
      <span class="k">return</span> <span class="n">employees</span><span class="o">.</span><span class="na">values</span><span class="o">();</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="n">Employee</span> <span class="nf">get</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">){</span>
      <span class="k">return</span> <span class="n">employees</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
   <span class="o">}</span>

   <span class="kd">public</span> <span class="kt">void</span> <span class="nf">delete</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">){</span>
      <span class="n">employees</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
   <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div></li>
</ul>
<h3 id="2功能清单">2、功能清单</h3>
<table>
<thead>
<tr>
<th>功能</th>
<th>URL 地址</th>
<th>请求方式</th>
</tr>
</thead>
<tbody>
<tr>
<td>访问首页√</td>
<td>/</td>
<td>GET</td>
</tr>
<tr>
<td>查询全部数据√</td>
<td>/employee</td>
<td>GET</td>
</tr>
<tr>
<td>删除√</td>
<td>/employee/2</td>
<td>DELETE</td>
</tr>
<tr>
<td>跳转到添加数据页面√</td>
<td>/toAdd</td>
<td>GET</td>
</tr>
<tr>
<td>执行保存√</td>
<td>/employee</td>
<td>POST</td>
</tr>
<tr>
<td>跳转到更新数据页面√</td>
<td>/employee/2</td>
<td>GET</td>
</tr>
<tr>
<td>执行更新√</td>
<td>/employee</td>
<td>PUT</td>
</tr>
</tbody>
</table>
<h3 id="3具体功能访问首页">3、具体功能：访问首页</h3>
<h5 id="a配置view-controller">a&gt;配置view-controller</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;mvc:view-controller</span> <span class="na">path=</span><span class="s">&#34;/&#34;</span> <span class="na">view-name=</span><span class="s">&#34;index&#34;</span><span class="nt">/&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b创建页面">b&gt;创建页面</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;en&#34;</span> <span class="na">xmlns:th</span><span class="o">=</span><span class="s">&#34;http://www.thymeleaf.org&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&#34;UTF-8&#34;</span> <span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Title<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>首页<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/employee}&#34;</span><span class="p">&gt;</span>访问员工信息<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4具体功能查询所有员工数据">4、具体功能：查询所有员工数据</h3>
<h5 id="a控制器方法">a&gt;控制器方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;/employee&#34;</span><span class="o">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">GET</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getEmployeeList</span><span class="o">(</span><span class="n">Model</span> <span class="n">model</span><span class="o">){</span>
    <span class="n">Collection</span><span class="o">&lt;</span><span class="n">Employee</span><span class="o">&gt;</span> <span class="n">employeeList</span> <span class="o">=</span> <span class="n">employeeDao</span><span class="o">.</span><span class="na">getAll</span><span class="o">();</span>
    <span class="n">model</span><span class="o">.</span><span class="na">addAttribute</span><span class="o">(</span><span class="s">&#34;employeeList&#34;</span><span class="o">,</span> <span class="n">employeeList</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;employee_list&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b创建employee_listhtml">b&gt;创建employee_list.html</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;en&#34;</span> <span class="na">xmlns:th</span><span class="o">=</span><span class="s">&#34;http://www.thymeleaf.org&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&#34;UTF-8&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Employee Info<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">th:src</span><span class="o">=</span><span class="s">&#34;@{/static/js/vue.js}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>

    <span class="p">&lt;</span><span class="nt">table</span> <span class="na">border</span><span class="o">=</span><span class="s">&#34;1&#34;</span> <span class="na">cellpadding</span><span class="o">=</span><span class="s">&#34;0&#34;</span> <span class="na">cellspacing</span><span class="o">=</span><span class="s">&#34;0&#34;</span> <span class="na">style</span><span class="o">=</span><span class="s">&#34;text-align: center;&#34;</span> <span class="na">id</span><span class="o">=</span><span class="s">&#34;dataTable&#34;</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span> <span class="na">colspan</span><span class="o">=</span><span class="s">&#34;5&#34;</span><span class="p">&gt;</span>Employee Info<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span><span class="p">&gt;</span>id<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span><span class="p">&gt;</span>lastName<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span><span class="p">&gt;</span>email<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span><span class="p">&gt;</span>gender<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">th</span><span class="p">&gt;</span>options(<span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/toAdd}&#34;</span><span class="p">&gt;</span>add<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>)<span class="p">&lt;/</span><span class="nt">th</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">th:each</span><span class="o">=</span><span class="s">&#34;employee : ${employeeList}&#34;</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">td</span> <span class="na">th:text</span><span class="o">=</span><span class="s">&#34;${employee.id}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">td</span> <span class="na">th:text</span><span class="o">=</span><span class="s">&#34;${employee.lastName}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">td</span> <span class="na">th:text</span><span class="o">=</span><span class="s">&#34;${employee.email}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">td</span> <span class="na">th:text</span><span class="o">=</span><span class="s">&#34;${employee.gender}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">td</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">a</span> <span class="na">class</span><span class="o">=</span><span class="s">&#34;deleteA&#34;</span> <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">&#34;deleteEmployee&#34;</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{&#39;/employee/&#39;+${employee.id}}&#34;</span><span class="p">&gt;</span>delete<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{&#39;/employee/&#39;+${employee.id}}&#34;</span><span class="p">&gt;</span>update<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
            <span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">table</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5具体功能删除">5、具体功能：删除</h3>
<h5 id="a创建处理delete请求方式的表单">a&gt;创建处理delete请求方式的表单</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="c">&lt;!-- 作用：通过超链接控制表单的提交，将post请求转换为delete请求 --&gt;</span>
<span class="p">&lt;</span><span class="nt">form</span> <span class="na">id</span><span class="o">=</span><span class="s">&#34;delete_form&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    <span class="c">&lt;!-- HiddenHttpMethodFilter要求：必须传输_method请求参数，并且值为最终的请求方式 --&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;hidden&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;_method&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;delete&#34;</span><span class="p">/&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b删除超链接绑定点击事件">b&gt;删除超链接绑定点击事件</h5>
<p>引入vue.js</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">th:src</span><span class="o">=</span><span class="s">&#34;@{/static/js/vue.js}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>删除超链接</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">class</span><span class="o">=</span><span class="s">&#34;deleteA&#34;</span> <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">&#34;deleteEmployee&#34;</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{&#39;/employee/&#39;+${employee.id}}&#34;</span><span class="p">&gt;</span>delete<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>通过vue处理点击事件</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span><span class="p">&gt;</span>
    <span class="kd">var</span> <span class="nx">vue</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Vue</span><span class="p">({</span>
        <span class="nx">el</span><span class="o">:</span><span class="s2">&#34;#dataTable&#34;</span><span class="p">,</span>
        <span class="nx">methods</span><span class="o">:</span><span class="p">{</span>
            <span class="c1">//event表示当前事件
</span><span class="c1"></span>            <span class="nx">deleteEmployee</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
                <span class="c1">//通过id获取表单标签
</span><span class="c1"></span>                <span class="kd">var</span> <span class="nx">delete_form</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">&#34;delete_form&#34;</span><span class="p">);</span>
                <span class="c1">//将触发事件的超链接的href属性为表单的action属性赋值
</span><span class="c1"></span>                <span class="nx">delete_form</span><span class="p">.</span><span class="nx">action</span> <span class="o">=</span> <span class="nx">event</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">href</span><span class="p">;</span>
                <span class="c1">//提交表单
</span><span class="c1"></span>                <span class="nx">delete_form</span><span class="p">.</span><span class="nx">submit</span><span class="p">();</span>
                <span class="c1">//阻止超链接的默认跳转行为
</span><span class="c1"></span>                <span class="nx">event</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">});</span>
<span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="c控制器方法">c&gt;控制器方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;/employee/{id}&#34;</span><span class="o">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">DELETE</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">deleteEmployee</span><span class="o">(</span><span class="nd">@PathVariable</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">)</span> <span class="n">Integer</span> <span class="n">id</span><span class="o">){</span>
    <span class="n">employeeDao</span><span class="o">.</span><span class="na">delete</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;redirect:/employee&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6具体功能跳转到添加数据页面">6、具体功能：跳转到添加数据页面</h3>
<h5 id="a配置view-controller-1">a&gt;配置view-controller</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;mvc:view-controller</span> <span class="na">path=</span><span class="s">&#34;/toAdd&#34;</span> <span class="na">view-name=</span><span class="s">&#34;employee_add&#34;</span><span class="nt">&gt;&lt;/mvc:view-controller&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b创建employee_addhtml">b&gt;创建employee_add.html</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;en&#34;</span> <span class="na">xmlns:th</span><span class="o">=</span><span class="s">&#34;http://www.thymeleaf.org&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&#34;UTF-8&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Add Employee<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">form</span> <span class="na">th:action</span><span class="o">=</span><span class="s">&#34;@{/employee}&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    lastName:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;lastName&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    email:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;email&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    gender:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;gender&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;1&#34;</span><span class="p">&gt;</span>male
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;gender&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;0&#34;</span><span class="p">&gt;</span>female<span class="p">&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;submit&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;add&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>

<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="7具体功能执行保存">7、具体功能：执行保存</h3>
<h5 id="a控制器方法-1">a&gt;控制器方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;/employee&#34;</span><span class="o">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">POST</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">addEmployee</span><span class="o">(</span><span class="n">Employee</span> <span class="n">employee</span><span class="o">){</span>
    <span class="n">employeeDao</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">employee</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;redirect:/employee&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="8具体功能跳转到更新数据页面">8、具体功能：跳转到更新数据页面</h3>
<h5 id="a修改超链接">a&gt;修改超链接</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{&#39;/employee/&#39;+${employee.id}}&#34;</span><span class="p">&gt;</span>update<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b控制器方法">b&gt;控制器方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;/employee/{id}&#34;</span><span class="o">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">GET</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getEmployeeById</span><span class="o">(</span><span class="nd">@PathVariable</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">)</span> <span class="n">Integer</span> <span class="n">id</span><span class="o">,</span> <span class="n">Model</span> <span class="n">model</span><span class="o">){</span>
    <span class="n">Employee</span> <span class="n">employee</span> <span class="o">=</span> <span class="n">employeeDao</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
    <span class="n">model</span><span class="o">.</span><span class="na">addAttribute</span><span class="o">(</span><span class="s">&#34;employee&#34;</span><span class="o">,</span> <span class="n">employee</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;employee_update&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="c创建employee_updatehtml">c&gt;创建employee_update.html</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&#34;en&#34;</span> <span class="na">xmlns:th</span><span class="o">=</span><span class="s">&#34;http://www.thymeleaf.org&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&#34;UTF-8&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Update Employee<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">form</span> <span class="na">th:action</span><span class="o">=</span><span class="s">&#34;@{/employee}&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;hidden&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;_method&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;put&#34;</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;hidden&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;id&#34;</span> <span class="na">th:value</span><span class="o">=</span><span class="s">&#34;${employee.id}&#34;</span><span class="p">&gt;</span>
    lastName:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;lastName&#34;</span> <span class="na">th:value</span><span class="o">=</span><span class="s">&#34;${employee.lastName}&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    email:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;email&#34;</span> <span class="na">th:value</span><span class="o">=</span><span class="s">&#34;${employee.email}&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    <span class="c">&lt;!--
</span><span class="c">        th:field=&#34;${employee.gender}&#34;可用于单选框或复选框的回显
</span><span class="c">        若单选框的value和employee.gender的值一致，则添加checked=&#34;checked&#34;属性
</span><span class="c">    --&gt;</span>
    gender:<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;gender&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;1&#34;</span> <span class="na">th:field</span><span class="o">=</span><span class="s">&#34;${employee.gender}&#34;</span><span class="p">&gt;</span>male
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;radio&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;gender&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;0&#34;</span> <span class="na">th:field</span><span class="o">=</span><span class="s">&#34;${employee.gender}&#34;</span><span class="p">&gt;</span>female<span class="p">&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;submit&#34;</span> <span class="na">value</span><span class="o">=</span><span class="s">&#34;update&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>

<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="9具体功能执行更新">9、具体功能：执行更新</h3>
<h5 id="a控制器方法-2">a&gt;控制器方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;/employee&#34;</span><span class="o">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="o">.</span><span class="na">PUT</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">updateEmployee</span><span class="o">(</span><span class="n">Employee</span> <span class="n">employee</span><span class="o">){</span>
    <span class="n">employeeDao</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">employee</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;redirect:/employee&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="八httpmessageconverter">八、HttpMessageConverter</h1>
<p>HttpMessageConverter，报文信息转换器，将请求报文转换为Java对象，或将Java对象转换为响应报文</p>
<p>HttpMessageConverter提供了两个注解和两个类型：@RequestBody，@ResponseBody，RequestEntity，</p>
<p>ResponseEntity</p>
<h3 id="1requestbody">1、@RequestBody</h3>
<p>@RequestBody可以获取请求体，需要在控制器方法设置一个形参，使用@RequestBody进行标识，当前请求的请求体就会为当前注解所标识的形参赋值</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">form</span> <span class="na">th:action</span><span class="o">=</span><span class="s">&#34;@{/testRequestBody}&#34;</span> <span class="na">method</span><span class="o">=</span><span class="s">&#34;post&#34;</span><span class="p">&gt;</span>
    用户名：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;username&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    密码：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;password&#34;</span> <span class="na">name</span><span class="o">=</span><span class="s">&#34;password&#34;</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;submit&#34;</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testRequestBody&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestBody</span><span class="o">(</span><span class="nd">@RequestBody</span> <span class="n">String</span> <span class="n">requestBody</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;requestBody:&#34;</span><span class="o">+</span><span class="n">requestBody</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>输出结果：</p>
<p>requestBody:username=admin&amp;password=123456</p>
<h3 id="2requestentity">2、RequestEntity</h3>
<p>RequestEntity封装请求报文的一种类型，需要在控制器方法的形参中设置该类型的形参，当前请求的请求报文就会赋值给该形参，可以通过getHeaders()获取请求头信息，通过getBody()获取请求体信息</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testRequestEntity&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testRequestEntity</span><span class="o">(</span><span class="n">RequestEntity</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">requestEntity</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;requestHeader:&#34;</span><span class="o">+</span><span class="n">requestEntity</span><span class="o">.</span><span class="na">getHeaders</span><span class="o">());</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;requestBody:&#34;</span><span class="o">+</span><span class="n">requestEntity</span><span class="o">.</span><span class="na">getBody</span><span class="o">());</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>输出结果：
requestHeader:[host:&ldquo;localhost:8080&rdquo;, connection:&ldquo;keep-alive&rdquo;, content-length:&ldquo;27&rdquo;, cache-control:&ldquo;max-age=0&rdquo;, sec-ch-ua:&rdquo;&rdquo; Not A;Brand&rdquo;;v=&ldquo;99&rdquo;, &ldquo;Chromium&rdquo;;v=&ldquo;90&rdquo;, &ldquo;Google Chrome&rdquo;;v=&ldquo;90&quot;&rdquo;, sec-ch-ua-mobile:&quot;?0&rdquo;, upgrade-insecure-requests:&ldquo;1&rdquo;, origin:&ldquo;http://localhost:8080&rdquo;, user-agent:&ldquo;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36&rdquo;]
requestBody:username=admin&amp;password=123</p>
<h3 id="3responsebody">3、@ResponseBody</h3>
<p>@ResponseBody用于标识一个控制器方法，可以将该方法的返回值直接作为响应报文的响应体响应到浏览器</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testResponseBody&#34;</span><span class="o">)</span>
<span class="nd">@ResponseBody</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testResponseBody</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>结果：浏览器页面显示success</p>
<h3 id="4springmvc处理json">4、SpringMVC处理json</h3>
<p>@ResponseBody处理json的步骤：</p>
<p>a&gt;导入jackson的依赖</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>com.fasterxml.jackson.core<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>jackson-databind<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>2.12.1<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>b&gt;在SpringMVC的核心配置文件中开启mvc的注解驱动，此时在HandlerAdaptor中会自动装配一个消息转换器：MappingJackson2HttpMessageConverter，可以将响应到浏览器的Java对象转换为Json格式的字符串</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">&lt;mvc:annotation-driven /&gt;
</code></pre></td></tr></table>
</div>
</div><p>c&gt;在处理器方法上使用@ResponseBody注解进行标识</p>
<p>d&gt;将Java对象直接作为控制器方法的返回值返回，就会自动转换为Json格式的字符串</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testResponseUser&#34;</span><span class="o">)</span>
<span class="nd">@ResponseBody</span>
<span class="kd">public</span> <span class="n">User</span> <span class="nf">testResponseUser</span><span class="o">(){</span>
    <span class="k">return</span> <span class="k">new</span> <span class="n">User</span><span class="o">(</span><span class="n">1001</span><span class="o">,</span><span class="s">&#34;admin&#34;</span><span class="o">,</span><span class="s">&#34;123456&#34;</span><span class="o">,</span><span class="n">23</span><span class="o">,</span><span class="s">&#34;男&#34;</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>浏览器的页面中展示的结果：</p>
<p>{&ldquo;id&rdquo;:1001,&ldquo;username&rdquo;:&ldquo;admin&rdquo;,&ldquo;password&rdquo;:&ldquo;123456&rdquo;,&ldquo;age&rdquo;:23,&ldquo;sex&rdquo;:&ldquo;男&rdquo;}</p>
<h3 id="5springmvc处理ajax">5、SpringMVC处理ajax</h3>
<p>a&gt;请求超链接：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&#34;app&#34;</span><span class="p">&gt;</span>
	<span class="p">&lt;</span><span class="nt">a</span> <span class="na">th:href</span><span class="o">=</span><span class="s">&#34;@{/testAjax}&#34;</span> <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">&#34;testAjax&#34;</span><span class="p">&gt;</span>testAjax<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;</span><span class="nt">br</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>b&gt;通过vue和axios处理点击事件：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">th:src</span><span class="o">=</span><span class="s">&#34;@{/static/js/vue.js}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span> <span class="na">th:src</span><span class="o">=</span><span class="s">&#34;@{/static/js/axios.min.js}&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&#34;text/javascript&#34;</span><span class="p">&gt;</span>
    <span class="kd">var</span> <span class="nx">vue</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Vue</span><span class="p">({</span>
        <span class="nx">el</span><span class="o">:</span><span class="s2">&#34;#app&#34;</span><span class="p">,</span>
        <span class="nx">methods</span><span class="o">:</span><span class="p">{</span>
            <span class="nx">testAjax</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
                <span class="nx">axios</span><span class="p">({</span>
                    <span class="nx">method</span><span class="o">:</span><span class="s2">&#34;post&#34;</span><span class="p">,</span>
                    <span class="nx">url</span><span class="o">:</span><span class="nx">event</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">href</span><span class="p">,</span>
                    <span class="nx">params</span><span class="o">:</span><span class="p">{</span>
                        <span class="nx">username</span><span class="o">:</span><span class="s2">&#34;admin&#34;</span><span class="p">,</span>
                        <span class="nx">password</span><span class="o">:</span><span class="s2">&#34;123456&#34;</span>
                    <span class="p">}</span>
                <span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">response</span><span class="p">)</span> <span class="p">{</span>
                    <span class="nx">alert</span><span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">data</span><span class="p">);</span>
                <span class="p">});</span>
                <span class="nx">event</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">});</span>
<span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>c&gt;控制器方法：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testAjax&#34;</span><span class="o">)</span>
<span class="nd">@ResponseBody</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testAjax</span><span class="o">(</span><span class="n">String</span> <span class="n">username</span><span class="o">,</span> <span class="n">String</span> <span class="n">password</span><span class="o">){</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&#34;username:&#34;</span><span class="o">+</span><span class="n">username</span><span class="o">+</span><span class="s">&#34;,password:&#34;</span><span class="o">+</span><span class="n">password</span><span class="o">);</span>
    <span class="k">return</span> <span class="s">&#34;hello,ajax&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6restcontroller注解">6、@RestController注解</h3>
<p>@RestController注解是springMVC提供的一个复合注解，标识在控制器的类上，就相当于为类添加了@Controller注解，并且为其中的每个方法添加了@ResponseBody注解</p>
<h3 id="7responseentity">7、ResponseEntity</h3>
<p>ResponseEntity用于控制器方法的返回值类型，该控制器方法的返回值就是响应到浏览器的响应报文</p>
<h1 id="九文件上传和下载">九、文件上传和下载</h1>
<h3 id="1文件下载">1、文件下载</h3>
<p>使用ResponseEntity实现下载文件的功能</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testDown&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">ResponseEntity</span><span class="o">&lt;</span><span class="kt">byte</span><span class="o">[]&gt;</span> <span class="nf">testResponseEntity</span><span class="o">(</span><span class="n">HttpSession</span> <span class="n">session</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
    <span class="c1">//获取ServletContext对象
</span><span class="c1"></span>    <span class="n">ServletContext</span> <span class="n">servletContext</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="na">getServletContext</span><span class="o">();</span>
    <span class="c1">//获取服务器中文件的真实路径
</span><span class="c1"></span>    <span class="n">String</span> <span class="n">realPath</span> <span class="o">=</span> <span class="n">servletContext</span><span class="o">.</span><span class="na">getRealPath</span><span class="o">(</span><span class="s">&#34;/static/img/1.jpg&#34;</span><span class="o">);</span>
    <span class="c1">//创建输入流
</span><span class="c1"></span>    <span class="n">InputStream</span> <span class="n">is</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileInputStream</span><span class="o">(</span><span class="n">realPath</span><span class="o">);</span>
    <span class="c1">//创建字节数组
</span><span class="c1"></span>    <span class="kt">byte</span><span class="o">[]</span> <span class="n">bytes</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="n">is</span><span class="o">.</span><span class="na">available</span><span class="o">()];</span>
    <span class="c1">//将流读到字节数组中
</span><span class="c1"></span>    <span class="n">is</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">bytes</span><span class="o">);</span>
    <span class="c1">//创建HttpHeaders对象设置响应头信息
</span><span class="c1"></span>    <span class="n">MultiValueMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">String</span><span class="o">&gt;</span> <span class="n">headers</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HttpHeaders</span><span class="o">();</span>
    <span class="c1">//设置要下载方式以及下载文件的名字
</span><span class="c1"></span>    <span class="n">headers</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">&#34;Content-Disposition&#34;</span><span class="o">,</span> <span class="s">&#34;attachment;filename=1.jpg&#34;</span><span class="o">);</span>
    <span class="c1">//设置响应状态码
</span><span class="c1"></span>    <span class="n">HttpStatus</span> <span class="n">statusCode</span> <span class="o">=</span> <span class="n">HttpStatus</span><span class="o">.</span><span class="na">OK</span><span class="o">;</span>
    <span class="c1">//创建ResponseEntity对象
</span><span class="c1"></span>    <span class="n">ResponseEntity</span><span class="o">&lt;</span><span class="kt">byte</span><span class="o">[]&gt;</span> <span class="n">responseEntity</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ResponseEntity</span><span class="o">&lt;&gt;(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">headers</span><span class="o">,</span> <span class="n">statusCode</span><span class="o">);</span>
    <span class="c1">//关闭输入流
</span><span class="c1"></span>    <span class="n">is</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
    <span class="k">return</span> <span class="n">responseEntity</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2文件上传">2、文件上传</h3>
<p>文件上传要求form表单的请求方式必须为post，并且添加属性enctype=&ldquo;multipart/form-data&rdquo;</p>
<p>SpringMVC中将上传的文件封装到MultipartFile对象中，通过此对象可以获取文件相关信息</p>
<p>上传步骤：</p>
<p>a&gt;添加依赖：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --&gt;</span>
<span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>commons-fileupload<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>commons-fileupload<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>1.3.1<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>b&gt;在SpringMVC的配置文件中添加配置：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="c">&lt;!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象--&gt;</span>
<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&#34;multipartResolver&#34;</span> <span class="na">class=</span><span class="s">&#34;org.springframework.web.multipart.commons.CommonsMultipartResolver&#34;</span><span class="nt">&gt;&lt;/bean&gt;</span>
</code></pre></td></tr></table>
</div>
</div><p>c&gt;控制器方法：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/testUp&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">testUp</span><span class="o">(</span><span class="n">MultipartFile</span> <span class="n">photo</span><span class="o">,</span> <span class="n">HttpSession</span> <span class="n">session</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
    <span class="c1">//获取上传的文件的文件名
</span><span class="c1"></span>    <span class="n">String</span> <span class="n">fileName</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="na">getOriginalFilename</span><span class="o">();</span>
    <span class="c1">//处理文件重名问题
</span><span class="c1"></span>    <span class="n">String</span> <span class="n">hzName</span> <span class="o">=</span> <span class="n">fileName</span><span class="o">.</span><span class="na">substring</span><span class="o">(</span><span class="n">fileName</span><span class="o">.</span><span class="na">lastIndexOf</span><span class="o">(</span><span class="s">&#34;.&#34;</span><span class="o">));</span>
    <span class="n">fileName</span> <span class="o">=</span> <span class="n">UUID</span><span class="o">.</span><span class="na">randomUUID</span><span class="o">().</span><span class="na">toString</span><span class="o">()</span> <span class="o">+</span> <span class="n">hzName</span><span class="o">;</span>
    <span class="c1">//获取服务器中photo目录的路径
</span><span class="c1"></span>    <span class="n">ServletContext</span> <span class="n">servletContext</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="na">getServletContext</span><span class="o">();</span>
    <span class="n">String</span> <span class="n">photoPath</span> <span class="o">=</span> <span class="n">servletContext</span><span class="o">.</span><span class="na">getRealPath</span><span class="o">(</span><span class="s">&#34;photo&#34;</span><span class="o">);</span>
    <span class="n">File</span> <span class="n">file</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="n">photoPath</span><span class="o">);</span>
    <span class="k">if</span><span class="o">(!</span><span class="n">file</span><span class="o">.</span><span class="na">exists</span><span class="o">()){</span>
        <span class="n">file</span><span class="o">.</span><span class="na">mkdir</span><span class="o">();</span>
    <span class="o">}</span>
    <span class="n">String</span> <span class="n">finalPath</span> <span class="o">=</span> <span class="n">photoPath</span> <span class="o">+</span> <span class="n">File</span><span class="o">.</span><span class="na">separator</span> <span class="o">+</span> <span class="n">fileName</span><span class="o">;</span>
    <span class="c1">//实现上传功能
</span><span class="c1"></span>    <span class="n">photo</span><span class="o">.</span><span class="na">transferTo</span><span class="o">(</span><span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="n">finalPath</span><span class="o">));</span>
    <span class="k">return</span> <span class="s">&#34;success&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="十拦截器">十、拦截器</h1>
<h3 id="1拦截器的配置">1、拦截器的配置</h3>
<p>SpringMVC中的拦截器用于拦截控制器方法的执行</p>
<p>SpringMVC中的拦截器需要实现HandlerInterceptor</p>
<p>SpringMVC的拦截器必须在SpringMVC的配置文件中进行配置：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&#34;com.atguigu.interceptor.FirstInterceptor&#34;</span><span class="nt">&gt;&lt;/bean&gt;</span>
<span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&#34;firstInterceptor&#34;</span><span class="nt">&gt;&lt;/ref&gt;</span>
<span class="c">&lt;!-- 以上两种配置方式都是对DispatcherServlet所处理的所有的请求进行拦截 --&gt;</span>
<span class="nt">&lt;mvc:interceptor&gt;</span>
    <span class="nt">&lt;mvc:mapping</span> <span class="na">path=</span><span class="s">&#34;/**&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;mvc:exclude-mapping</span> <span class="na">path=</span><span class="s">&#34;/testRequestEntity&#34;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&#34;firstInterceptor&#34;</span><span class="nt">&gt;&lt;/ref&gt;</span>
<span class="nt">&lt;/mvc:interceptor&gt;</span>
<span class="c">&lt;!-- 
</span><span class="c">	以上配置方式可以通过ref或bean标签设置拦截器，通过mvc:mapping设置需要拦截的请求，通过mvc:exclude-mapping设置需要排除的请求，即不需要拦截的请求
</span><span class="c">--&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2拦截器的三个抽象方法">2、拦截器的三个抽象方法</h3>
<p>SpringMVC中的拦截器有三个抽象方法：</p>
<p>preHandle：控制器方法执行之前执行preHandle()，其boolean类型的返回值表示是否拦截或放行，返回true为放行，即调用控制器方法；返回false表示拦截，即不调用控制器方法</p>
<p>postHandle：控制器方法执行之后执行postHandle()</p>
<p>afterComplation：处理完视图和模型数据，渲染视图完毕之后执行afterComplation()</p>
<h3 id="3多个拦截器的执行顺序">3、多个拦截器的执行顺序</h3>
<p>a&gt;若每个拦截器的preHandle()都返回true</p>
<p>此时多个拦截器的执行顺序和拦截器在SpringMVC的配置文件的配置顺序有关：</p>
<p>preHandle()会按照配置的顺序执行，而postHandle()和afterComplation()会按照配置的反序执行</p>
<p>b&gt;若某个拦截器的preHandle()返回了false</p>
<p>preHandle()返回false和它之前的拦截器的preHandle()都会执行，postHandle()都不执行，返回false的拦截器之前的拦截器的afterComplation()会执行</p>
<h1 id="十一异常处理器">十一、异常处理器</h1>
<h3 id="1基于配置的异常处理">1、基于配置的异常处理</h3>
<p>SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口：HandlerExceptionResolver</p>
<p>HandlerExceptionResolver接口的实现类有：DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver</p>
<p>SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver，使用方式：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;bean</span> <span class="na">class=</span><span class="s">&#34;org.springframework.web.servlet.handler.SimpleMappingExceptionResolver&#34;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;exceptionMappings&#34;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;props&gt;</span>
        	<span class="c">&lt;!--
</span><span class="c">        		properties的键表示处理器方法执行过程中出现的异常
</span><span class="c">        		properties的值表示若出现指定异常时，设置一个新的视图名称，跳转到指定页面
</span><span class="c">        	--&gt;</span>
            <span class="nt">&lt;prop</span> <span class="na">key=</span><span class="s">&#34;java.lang.ArithmeticException&#34;</span><span class="nt">&gt;</span>error<span class="nt">&lt;/prop&gt;</span>
        <span class="nt">&lt;/props&gt;</span>
    <span class="nt">&lt;/property&gt;</span>
    <span class="c">&lt;!--
</span><span class="c">    	exceptionAttribute属性设置一个属性名，将出现的异常信息在请求域中进行共享
</span><span class="c">    --&gt;</span>
    <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&#34;exceptionAttribute&#34;</span> <span class="na">value=</span><span class="s">&#34;ex&#34;</span><span class="nt">&gt;&lt;/property&gt;</span>
<span class="nt">&lt;/bean&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2基于注解的异常处理">2、基于注解的异常处理</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="c1">//@ControllerAdvice将当前类标识为异常处理的组件
</span><span class="c1"></span><span class="nd">@ControllerAdvice</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExceptionController</span> <span class="o">{</span>

    <span class="c1">//@ExceptionHandler用于设置所标识方法处理的异常
</span><span class="c1"></span>    <span class="nd">@ExceptionHandler</span><span class="o">(</span><span class="n">ArithmeticException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
    <span class="c1">//ex表示当前请求处理中出现的异常对象
</span><span class="c1"></span>    <span class="kd">public</span> <span class="n">String</span> <span class="nf">handleArithmeticException</span><span class="o">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="o">,</span> <span class="n">Model</span> <span class="n">model</span><span class="o">){</span>
        <span class="n">model</span><span class="o">.</span><span class="na">addAttribute</span><span class="o">(</span><span class="s">&#34;ex&#34;</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
        <span class="k">return</span> <span class="s">&#34;error&#34;</span><span class="o">;</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="十二注解配置springmvc">十二、注解配置SpringMVC</h1>
<p>使用配置类和注解代替web.xml和SpringMVC配置文件的功能</p>
<h3 id="1创建初始化类代替webxml">1、创建初始化类，代替web.xml</h3>
<p>在Servlet3.0环境中，容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类，如果找到的话就用它来配置Servlet容器。
Spring提供了这个接口的实现，名为SpringServletContainerInitializer，这个类反过来又会查找实现WebApplicationInitializer的类并将配置的任务交给它们来完成。Spring3.2引入了一个便利的WebApplicationInitializer基础实现，名为AbstractAnnotationConfigDispatcherServletInitializer，当我们的类扩展了AbstractAnnotationConfigDispatcherServletInitializer并将其部署到Servlet3.0容器的时候，容器会自动发现它，并用它来配置Servlet上下文。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">WebInit</span> <span class="kd">extends</span> <span class="n">AbstractAnnotationConfigDispatcherServletInitializer</span> <span class="o">{</span>

    <span class="cm">/**
</span><span class="cm">     * 指定spring的配置类
</span><span class="cm">     * @return
</span><span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">Class</span><span class="o">&lt;?&gt;[]</span> <span class="n">getRootConfigClasses</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">Class</span><span class="o">[]{</span><span class="n">SpringConfig</span><span class="o">.</span><span class="na">class</span><span class="o">};</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 指定SpringMVC的配置类
</span><span class="cm">     * @return
</span><span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">Class</span><span class="o">&lt;?&gt;[]</span> <span class="n">getServletConfigClasses</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">Class</span><span class="o">[]{</span><span class="n">WebConfig</span><span class="o">.</span><span class="na">class</span><span class="o">};</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 指定DispatcherServlet的映射规则，即url-pattern
</span><span class="cm">     * @return
</span><span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">String</span><span class="o">[]</span> <span class="nf">getServletMappings</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">String</span><span class="o">[]{</span><span class="s">&#34;/&#34;</span><span class="o">};</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 添加过滤器
</span><span class="cm">     * @return
</span><span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">Filter</span><span class="o">[]</span> <span class="nf">getServletFilters</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">CharacterEncodingFilter</span> <span class="n">encodingFilter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CharacterEncodingFilter</span><span class="o">();</span>
        <span class="n">encodingFilter</span><span class="o">.</span><span class="na">setEncoding</span><span class="o">(</span><span class="s">&#34;UTF-8&#34;</span><span class="o">);</span>
        <span class="n">encodingFilter</span><span class="o">.</span><span class="na">setForceRequestEncoding</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
        <span class="n">HiddenHttpMethodFilter</span> <span class="n">hiddenHttpMethodFilter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HiddenHttpMethodFilter</span><span class="o">();</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">Filter</span><span class="o">[]{</span><span class="n">encodingFilter</span><span class="o">,</span> <span class="n">hiddenHttpMethodFilter</span><span class="o">};</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2创建springconfig配置类代替spring的配置文件">2、创建SpringConfig配置类，代替spring的配置文件</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Configuration</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SpringConfig</span> <span class="o">{</span>
	<span class="c1">//ssm整合之后，spring的配置信息写在此类中
</span><span class="c1"></span><span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3创建webconfig配置类代替springmvc的配置文件">3、创建WebConfig配置类，代替SpringMVC的配置文件</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span><span class="lnt">79
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Configuration</span>
<span class="c1">//扫描组件
</span><span class="c1"></span><span class="nd">@ComponentScan</span><span class="o">(</span><span class="s">&#34;com.atguigu.mvc.controller&#34;</span><span class="o">)</span>
<span class="c1">//开启MVC注解驱动
</span><span class="c1"></span><span class="nd">@EnableWebMvc</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">WebConfig</span> <span class="kd">implements</span> <span class="n">WebMvcConfigurer</span> <span class="o">{</span>

    <span class="c1">//使用默认的servlet处理静态资源
</span><span class="c1"></span>    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">configureDefaultServletHandling</span><span class="o">(</span><span class="n">DefaultServletHandlerConfigurer</span> <span class="n">configurer</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">configurer</span><span class="o">.</span><span class="na">enable</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="c1">//配置文件上传解析器
</span><span class="c1"></span>    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">CommonsMultipartResolver</span> <span class="nf">multipartResolver</span><span class="o">(){</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">CommonsMultipartResolver</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="c1">//配置拦截器
</span><span class="c1"></span>    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addInterceptors</span><span class="o">(</span><span class="n">InterceptorRegistry</span> <span class="n">registry</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">FirstInterceptor</span> <span class="n">firstInterceptor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirstInterceptor</span><span class="o">();</span>
        <span class="n">registry</span><span class="o">.</span><span class="na">addInterceptor</span><span class="o">(</span><span class="n">firstInterceptor</span><span class="o">).</span><span class="na">addPathPatterns</span><span class="o">(</span><span class="s">&#34;/**&#34;</span><span class="o">);</span>
    <span class="o">}</span>
    
    <span class="c1">//配置视图控制
</span><span class="c1"></span>    
    <span class="cm">/*@Override
</span><span class="cm">    public void addViewControllers(ViewControllerRegistry registry) {
</span><span class="cm">        registry.addViewController(&#34;/&#34;).setViewName(&#34;index&#34;);
</span><span class="cm">    }*/</span>
    
    <span class="c1">//配置异常映射
</span><span class="c1"></span>    <span class="cm">/*@Override
</span><span class="cm">    public void configureHandlerExceptionResolvers(List&lt;HandlerExceptionResolver&gt; resolvers) {
</span><span class="cm">        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
</span><span class="cm">        Properties prop = new Properties();
</span><span class="cm">        prop.setProperty(&#34;java.lang.ArithmeticException&#34;, &#34;error&#34;);
</span><span class="cm">        //设置异常映射
</span><span class="cm">        exceptionResolver.setExceptionMappings(prop);
</span><span class="cm">        //设置共享异常信息的键
</span><span class="cm">        exceptionResolver.setExceptionAttribute(&#34;ex&#34;);
</span><span class="cm">        resolvers.add(exceptionResolver);
</span><span class="cm">    }*/</span>

    <span class="c1">//配置生成模板解析器
</span><span class="c1"></span>    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">ITemplateResolver</span> <span class="nf">templateResolver</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">WebApplicationContext</span> <span class="n">webApplicationContext</span> <span class="o">=</span> <span class="n">ContextLoader</span><span class="o">.</span><span class="na">getCurrentWebApplicationContext</span><span class="o">();</span>
        <span class="c1">// ServletContextTemplateResolver需要一个ServletContext作为构造参数，可通过WebApplicationContext 的方法获得
</span><span class="c1"></span>        <span class="n">ServletContextTemplateResolver</span> <span class="n">templateResolver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ServletContextTemplateResolver</span><span class="o">(</span>
                <span class="n">webApplicationContext</span><span class="o">.</span><span class="na">getServletContext</span><span class="o">());</span>
        <span class="n">templateResolver</span><span class="o">.</span><span class="na">setPrefix</span><span class="o">(</span><span class="s">&#34;/WEB-INF/templates/&#34;</span><span class="o">);</span>
        <span class="n">templateResolver</span><span class="o">.</span><span class="na">setSuffix</span><span class="o">(</span><span class="s">&#34;.html&#34;</span><span class="o">);</span>
        <span class="n">templateResolver</span><span class="o">.</span><span class="na">setCharacterEncoding</span><span class="o">(</span><span class="s">&#34;UTF-8&#34;</span><span class="o">);</span>
        <span class="n">templateResolver</span><span class="o">.</span><span class="na">setTemplateMode</span><span class="o">(</span><span class="n">TemplateMode</span><span class="o">.</span><span class="na">HTML</span><span class="o">);</span>
        <span class="k">return</span> <span class="n">templateResolver</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="c1">//生成模板引擎并为模板引擎注入模板解析器
</span><span class="c1"></span>    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">SpringTemplateEngine</span> <span class="nf">templateEngine</span><span class="o">(</span><span class="n">ITemplateResolver</span> <span class="n">templateResolver</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">SpringTemplateEngine</span> <span class="n">templateEngine</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SpringTemplateEngine</span><span class="o">();</span>
        <span class="n">templateEngine</span><span class="o">.</span><span class="na">setTemplateResolver</span><span class="o">(</span><span class="n">templateResolver</span><span class="o">);</span>
        <span class="k">return</span> <span class="n">templateEngine</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="c1">//生成视图解析器并未解析器注入模板引擎
</span><span class="c1"></span>    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">ViewResolver</span> <span class="nf">viewResolver</span><span class="o">(</span><span class="n">SpringTemplateEngine</span> <span class="n">templateEngine</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">ThymeleafViewResolver</span> <span class="n">viewResolver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ThymeleafViewResolver</span><span class="o">();</span>
        <span class="n">viewResolver</span><span class="o">.</span><span class="na">setCharacterEncoding</span><span class="o">(</span><span class="s">&#34;UTF-8&#34;</span><span class="o">);</span>
        <span class="n">viewResolver</span><span class="o">.</span><span class="na">setTemplateEngine</span><span class="o">(</span><span class="n">templateEngine</span><span class="o">);</span>
        <span class="k">return</span> <span class="n">viewResolver</span><span class="o">;</span>
    <span class="o">}</span>


<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4测试功能">4、测试功能</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@RequestMapping</span><span class="o">(</span><span class="s">&#34;/&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">index</span><span class="o">(){</span>
    <span class="k">return</span> <span class="s">&#34;index&#34;</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="十三springmvc执行流程">十三、SpringMVC执行流程</h1>
<h3 id="1springmvc常用组件">1、SpringMVC常用组件</h3>
<ul>
<li>DispatcherServlet：<strong>前端控制器</strong>，不需要工程师开发，由框架提供</li>
</ul>
<p>作用：统一处理请求和响应，整个流程控制的中心，由它调用其它组件处理用户的请求</p>
<ul>
<li>HandlerMapping：<strong>处理器映射器</strong>，不需要工程师开发，由框架提供</li>
</ul>
<p>作用：根据请求的url、method等信息查找Handler，即控制器方法</p>
<ul>
<li>Handler：<strong>处理器</strong>，需要工程师开发</li>
</ul>
<p>作用：在DispatcherServlet的控制下Handler对具体的用户请求进行处理</p>
<ul>
<li>HandlerAdapter：<strong>处理器适配器</strong>，不需要工程师开发，由框架提供</li>
</ul>
<p>作用：通过HandlerAdapter对处理器（控制器方法）进行执行</p>
<ul>
<li>ViewResolver：<strong>视图解析器</strong>，不需要工程师开发，由框架提供</li>
</ul>
<p>作用：进行视图解析，得到相应的视图，例如：ThymeleafView、InternalResourceView、RedirectView</p>
<ul>
<li>View：<strong>视图</strong></li>
</ul>
<p>作用：将模型数据通过页面展示给用户</p>
<h3 id="2dispatcherservlet初始化过程">2、DispatcherServlet初始化过程</h3>
<p>DispatcherServlet 本质上是一个 Servlet，所以天然的遵循 Servlet 的生命周期。所以宏观上是 Servlet 生命周期来进行调度。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img005.png"
        data-srcset="img/img005.png, img/img005.png 1.5x, img/img005.png 2x"
        data-sizes="auto"
        alt="img/img005.png"
        title="images" /></p>
<h5 id="a初始化webapplicationcontext">a&gt;初始化WebApplicationContext</h5>
<p>所在类：org.springframework.web.servlet.FrameworkServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">protected</span> <span class="n">WebApplicationContext</span> <span class="nf">initWebApplicationContext</span><span class="o">()</span> <span class="o">{</span>
    <span class="n">WebApplicationContext</span> <span class="n">rootContext</span> <span class="o">=</span>
        <span class="n">WebApplicationContextUtils</span><span class="o">.</span><span class="na">getWebApplicationContext</span><span class="o">(</span><span class="n">getServletContext</span><span class="o">());</span>
    <span class="n">WebApplicationContext</span> <span class="n">wac</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

    <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">webApplicationContext</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// A context instance was injected at construction time -&gt; use it
</span><span class="c1"></span>        <span class="n">wac</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">webApplicationContext</span><span class="o">;</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">wac</span> <span class="k">instanceof</span> <span class="n">ConfigurableWebApplicationContext</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">ConfigurableWebApplicationContext</span> <span class="n">cwac</span> <span class="o">=</span> <span class="o">(</span><span class="n">ConfigurableWebApplicationContext</span><span class="o">)</span> <span class="n">wac</span><span class="o">;</span>
            <span class="k">if</span> <span class="o">(!</span><span class="n">cwac</span><span class="o">.</span><span class="na">isActive</span><span class="o">())</span> <span class="o">{</span>
                <span class="c1">// The context has not yet been refreshed -&gt; provide services such as
</span><span class="c1"></span>                <span class="c1">// setting the parent context, setting the application context id, etc
</span><span class="c1"></span>                <span class="k">if</span> <span class="o">(</span><span class="n">cwac</span><span class="o">.</span><span class="na">getParent</span><span class="o">()</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                    <span class="c1">// The context instance was injected without an explicit parent -&gt; set
</span><span class="c1"></span>                    <span class="c1">// the root application context (if any; may be null) as the parent
</span><span class="c1"></span>                    <span class="n">cwac</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="n">rootContext</span><span class="o">);</span>
                <span class="o">}</span>
                <span class="n">configureAndRefreshWebApplicationContext</span><span class="o">(</span><span class="n">cwac</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">wac</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// No context instance was injected at construction time -&gt; see if one
</span><span class="c1"></span>        <span class="c1">// has been registered in the servlet context. If one exists, it is assumed
</span><span class="c1"></span>        <span class="c1">// that the parent context (if any) has already been set and that the
</span><span class="c1"></span>        <span class="c1">// user has performed any initialization such as setting the context id
</span><span class="c1"></span>        <span class="n">wac</span> <span class="o">=</span> <span class="n">findWebApplicationContext</span><span class="o">();</span>
    <span class="o">}</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">wac</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// No context instance is defined for this servlet -&gt; create a local one
</span><span class="c1"></span>        <span class="c1">// 创建WebApplicationContext
</span><span class="c1"></span>        <span class="n">wac</span> <span class="o">=</span> <span class="n">createWebApplicationContext</span><span class="o">(</span><span class="n">rootContext</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="k">if</span> <span class="o">(!</span><span class="k">this</span><span class="o">.</span><span class="na">refreshEventReceived</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// Either the context is not a ConfigurableApplicationContext with refresh
</span><span class="c1"></span>        <span class="c1">// support or the context injected at construction time had already been
</span><span class="c1"></span>        <span class="c1">// refreshed -&gt; trigger initial onRefresh manually here.
</span><span class="c1"></span>        <span class="kd">synchronized</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">onRefreshMonitor</span><span class="o">)</span> <span class="o">{</span>
            <span class="c1">// 刷新WebApplicationContext
</span><span class="c1"></span>            <span class="n">onRefresh</span><span class="o">(</span><span class="n">wac</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">publishContext</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// Publish the context as a servlet context attribute.
</span><span class="c1"></span>        <span class="c1">// 将IOC容器在应用域共享
</span><span class="c1"></span>        <span class="n">String</span> <span class="n">attrName</span> <span class="o">=</span> <span class="n">getServletContextAttributeName</span><span class="o">();</span>
        <span class="n">getServletContext</span><span class="o">().</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">attrName</span><span class="o">,</span> <span class="n">wac</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="k">return</span> <span class="n">wac</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="b创建webapplicationcontext">b&gt;创建WebApplicationContext</h5>
<p>所在类：org.springframework.web.servlet.FrameworkServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">protected</span> <span class="n">WebApplicationContext</span> <span class="nf">createWebApplicationContext</span><span class="o">(</span><span class="nd">@Nullable</span> <span class="n">ApplicationContext</span> <span class="n">parent</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">contextClass</span> <span class="o">=</span> <span class="n">getContextClass</span><span class="o">();</span>
    <span class="k">if</span> <span class="o">(!</span><span class="n">ConfigurableWebApplicationContext</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">isAssignableFrom</span><span class="o">(</span><span class="n">contextClass</span><span class="o">))</span> <span class="o">{</span>
        <span class="k">throw</span> <span class="k">new</span> <span class="n">ApplicationContextException</span><span class="o">(</span>
            <span class="s">&#34;Fatal initialization error in servlet with name &#39;&#34;</span> <span class="o">+</span> <span class="n">getServletName</span><span class="o">()</span> <span class="o">+</span>
            <span class="s">&#34;&#39;: custom WebApplicationContext class [&#34;</span> <span class="o">+</span> <span class="n">contextClass</span><span class="o">.</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span>
            <span class="s">&#34;] is not of type ConfigurableWebApplicationContext&#34;</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="c1">// 通过反射创建 IOC 容器对象
</span><span class="c1"></span>    <span class="n">ConfigurableWebApplicationContext</span> <span class="n">wac</span> <span class="o">=</span>
        <span class="o">(</span><span class="n">ConfigurableWebApplicationContext</span><span class="o">)</span> <span class="n">BeanUtils</span><span class="o">.</span><span class="na">instantiateClass</span><span class="o">(</span><span class="n">contextClass</span><span class="o">);</span>

    <span class="n">wac</span><span class="o">.</span><span class="na">setEnvironment</span><span class="o">(</span><span class="n">getEnvironment</span><span class="o">());</span>
    <span class="c1">// 设置父容器
</span><span class="c1"></span>    <span class="n">wac</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="n">parent</span><span class="o">);</span>
    <span class="n">String</span> <span class="n">configLocation</span> <span class="o">=</span> <span class="n">getContextConfigLocation</span><span class="o">();</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">configLocation</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">wac</span><span class="o">.</span><span class="na">setConfigLocation</span><span class="o">(</span><span class="n">configLocation</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="n">configureAndRefreshWebApplicationContext</span><span class="o">(</span><span class="n">wac</span><span class="o">);</span>

    <span class="k">return</span> <span class="n">wac</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="cdispatcherservlet初始化策略">c&gt;DispatcherServlet初始化策略</h5>
<p>FrameworkServlet创建WebApplicationContext后，刷新容器，调用onRefresh(wac)，此方法在DispatcherServlet中进行了重写，调用了initStrategies(context)方法，初始化策略，即初始化DispatcherServlet的各个组件</p>
<p>所在类：org.springframework.web.servlet.DispatcherServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">protected</span> <span class="kt">void</span> <span class="nf">initStrategies</span><span class="o">(</span><span class="n">ApplicationContext</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
   <span class="n">initMultipartResolver</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initLocaleResolver</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initThemeResolver</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initHandlerMappings</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initHandlerAdapters</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initHandlerExceptionResolvers</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initRequestToViewNameTranslator</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initViewResolvers</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
   <span class="n">initFlashMapManager</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3dispatcherservlet调用组件处理请求">3、DispatcherServlet调用组件处理请求</h3>
<h5 id="aprocessrequest">a&gt;processRequest()</h5>
<p>FrameworkServlet重写HttpServlet中的service()和doXxx()，这些方法中调用了processRequest(request, response)</p>
<p>所在类：org.springframework.web.servlet.FrameworkServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">protected</span> <span class="kd">final</span> <span class="kt">void</span> <span class="nf">processRequest</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">,</span> <span class="n">HttpServletResponse</span> <span class="n">response</span><span class="o">)</span>
    <span class="kd">throws</span> <span class="n">ServletException</span><span class="o">,</span> <span class="n">IOException</span> <span class="o">{</span>

    <span class="kt">long</span> <span class="n">startTime</span> <span class="o">=</span> <span class="n">System</span><span class="o">.</span><span class="na">currentTimeMillis</span><span class="o">();</span>
    <span class="n">Throwable</span> <span class="n">failureCause</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

    <span class="n">LocaleContext</span> <span class="n">previousLocaleContext</span> <span class="o">=</span> <span class="n">LocaleContextHolder</span><span class="o">.</span><span class="na">getLocaleContext</span><span class="o">();</span>
    <span class="n">LocaleContext</span> <span class="n">localeContext</span> <span class="o">=</span> <span class="n">buildLocaleContext</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>

    <span class="n">RequestAttributes</span> <span class="n">previousAttributes</span> <span class="o">=</span> <span class="n">RequestContextHolder</span><span class="o">.</span><span class="na">getRequestAttributes</span><span class="o">();</span>
    <span class="n">ServletRequestAttributes</span> <span class="n">requestAttributes</span> <span class="o">=</span> <span class="n">buildRequestAttributes</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">previousAttributes</span><span class="o">);</span>

    <span class="n">WebAsyncManager</span> <span class="n">asyncManager</span> <span class="o">=</span> <span class="n">WebAsyncUtils</span><span class="o">.</span><span class="na">getAsyncManager</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
    <span class="n">asyncManager</span><span class="o">.</span><span class="na">registerCallableInterceptor</span><span class="o">(</span><span class="n">FrameworkServlet</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">RequestBindingInterceptor</span><span class="o">());</span>

    <span class="n">initContextHolders</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">localeContext</span><span class="o">,</span> <span class="n">requestAttributes</span><span class="o">);</span>

    <span class="k">try</span> <span class="o">{</span>
		<span class="c1">// 执行服务，doService()是一个抽象方法，在DispatcherServlet中进行了重写
</span><span class="c1"></span>        <span class="n">doService</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="k">catch</span> <span class="o">(</span><span class="n">ServletException</span> <span class="o">|</span> <span class="n">IOException</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">failureCause</span> <span class="o">=</span> <span class="n">ex</span><span class="o">;</span>
        <span class="k">throw</span> <span class="n">ex</span><span class="o">;</span>
    <span class="o">}</span>
    <span class="k">catch</span> <span class="o">(</span><span class="n">Throwable</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">failureCause</span> <span class="o">=</span> <span class="n">ex</span><span class="o">;</span>
        <span class="k">throw</span> <span class="k">new</span> <span class="n">NestedServletException</span><span class="o">(</span><span class="s">&#34;Request processing failed&#34;</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="k">finally</span> <span class="o">{</span>
        <span class="n">resetContextHolders</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">previousLocaleContext</span><span class="o">,</span> <span class="n">previousAttributes</span><span class="o">);</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">requestAttributes</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">requestAttributes</span><span class="o">.</span><span class="na">requestCompleted</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="n">logResult</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">failureCause</span><span class="o">,</span> <span class="n">asyncManager</span><span class="o">);</span>
        <span class="n">publishRequestHandledEvent</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">startTime</span><span class="o">,</span> <span class="n">failureCause</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="bdoservice">b&gt;doService()</h5>
<p>所在类：org.springframework.web.servlet.DispatcherServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Override</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">doService</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">,</span> <span class="n">HttpServletResponse</span> <span class="n">response</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
    <span class="n">logRequest</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>

    <span class="c1">// Keep a snapshot of the request attributes in case of an include,
</span><span class="c1"></span>    <span class="c1">// to be able to restore the original attributes after the include.
</span><span class="c1"></span>    <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span> <span class="n">attributesSnapshot</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">WebUtils</span><span class="o">.</span><span class="na">isIncludeRequest</span><span class="o">(</span><span class="n">request</span><span class="o">))</span> <span class="o">{</span>
        <span class="n">attributesSnapshot</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span>
        <span class="n">Enumeration</span><span class="o">&lt;?&gt;</span> <span class="n">attrNames</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="na">getAttributeNames</span><span class="o">();</span>
        <span class="k">while</span> <span class="o">(</span><span class="n">attrNames</span><span class="o">.</span><span class="na">hasMoreElements</span><span class="o">())</span> <span class="o">{</span>
            <span class="n">String</span> <span class="n">attrName</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">attrNames</span><span class="o">.</span><span class="na">nextElement</span><span class="o">();</span>
            <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">cleanupAfterInclude</span> <span class="o">||</span> <span class="n">attrName</span><span class="o">.</span><span class="na">startsWith</span><span class="o">(</span><span class="n">DEFAULT_STRATEGIES_PREFIX</span><span class="o">))</span> <span class="o">{</span>
                <span class="n">attributesSnapshot</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">attrName</span><span class="o">,</span> <span class="n">request</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="n">attrName</span><span class="o">));</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="c1">// Make framework objects available to handlers and view objects.
</span><span class="c1"></span>    <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">WEB_APPLICATION_CONTEXT_ATTRIBUTE</span><span class="o">,</span> <span class="n">getWebApplicationContext</span><span class="o">());</span>
    <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">LOCALE_RESOLVER_ATTRIBUTE</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">localeResolver</span><span class="o">);</span>
    <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">THEME_RESOLVER_ATTRIBUTE</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">themeResolver</span><span class="o">);</span>
    <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">THEME_SOURCE_ATTRIBUTE</span><span class="o">,</span> <span class="n">getThemeSource</span><span class="o">());</span>

    <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">flashMapManager</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">FlashMap</span> <span class="n">inputFlashMap</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">flashMapManager</span><span class="o">.</span><span class="na">retrieveAndUpdate</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">inputFlashMap</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">INPUT_FLASH_MAP_ATTRIBUTE</span><span class="o">,</span> <span class="n">Collections</span><span class="o">.</span><span class="na">unmodifiableMap</span><span class="o">(</span><span class="n">inputFlashMap</span><span class="o">));</span>
        <span class="o">}</span>
        <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">OUTPUT_FLASH_MAP_ATTRIBUTE</span><span class="o">,</span> <span class="k">new</span> <span class="n">FlashMap</span><span class="o">());</span>
        <span class="n">request</span><span class="o">.</span><span class="na">setAttribute</span><span class="o">(</span><span class="n">FLASH_MAP_MANAGER_ATTRIBUTE</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">flashMapManager</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="n">RequestPath</span> <span class="n">requestPath</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
    <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">parseRequestPath</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">ServletRequestPathUtils</span><span class="o">.</span><span class="na">hasParsedRequestPath</span><span class="o">(</span><span class="n">request</span><span class="o">))</span> <span class="o">{</span>
        <span class="n">requestPath</span> <span class="o">=</span> <span class="n">ServletRequestPathUtils</span><span class="o">.</span><span class="na">parseAndCache</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="k">try</span> <span class="o">{</span>
        <span class="c1">// 处理请求和响应
</span><span class="c1"></span>        <span class="n">doDispatch</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="k">finally</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(!</span><span class="n">WebAsyncUtils</span><span class="o">.</span><span class="na">getAsyncManager</span><span class="o">(</span><span class="n">request</span><span class="o">).</span><span class="na">isConcurrentHandlingStarted</span><span class="o">())</span> <span class="o">{</span>
            <span class="c1">// Restore the original attribute snapshot, in case of an include.
</span><span class="c1"></span>            <span class="k">if</span> <span class="o">(</span><span class="n">attributesSnapshot</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">restoreAttributesAfterInclude</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">attributesSnapshot</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">requestPath</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">ServletRequestPathUtils</span><span class="o">.</span><span class="na">clearParsedRequestPath</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="cdodispatch">c&gt;doDispatch()</h5>
<p>所在类：org.springframework.web.servlet.DispatcherServlet</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span><span class="lnt">79
</span><span class="lnt">80
</span><span class="lnt">81
</span><span class="lnt">82
</span><span class="lnt">83
</span><span class="lnt">84
</span><span class="lnt">85
</span><span class="lnt">86
</span><span class="lnt">87
</span><span class="lnt">88
</span><span class="lnt">89
</span><span class="lnt">90
</span><span class="lnt">91
</span><span class="lnt">92
</span><span class="lnt">93
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">protected</span> <span class="kt">void</span> <span class="nf">doDispatch</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">,</span> <span class="n">HttpServletResponse</span> <span class="n">response</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
    <span class="n">HttpServletRequest</span> <span class="n">processedRequest</span> <span class="o">=</span> <span class="n">request</span><span class="o">;</span>
    <span class="n">HandlerExecutionChain</span> <span class="n">mappedHandler</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
    <span class="kt">boolean</span> <span class="n">multipartRequestParsed</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>

    <span class="n">WebAsyncManager</span> <span class="n">asyncManager</span> <span class="o">=</span> <span class="n">WebAsyncUtils</span><span class="o">.</span><span class="na">getAsyncManager</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>

    <span class="k">try</span> <span class="o">{</span>
        <span class="n">ModelAndView</span> <span class="n">mv</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
        <span class="n">Exception</span> <span class="n">dispatchException</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

        <span class="k">try</span> <span class="o">{</span>
            <span class="n">processedRequest</span> <span class="o">=</span> <span class="n">checkMultipart</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
            <span class="n">multipartRequestParsed</span> <span class="o">=</span> <span class="o">(</span><span class="n">processedRequest</span> <span class="o">!=</span> <span class="n">request</span><span class="o">);</span>

            <span class="c1">// Determine handler for the current request.
</span><span class="c1"></span>            <span class="cm">/*
</span><span class="cm">            	mappedHandler：调用链
</span><span class="cm">                包含handler、interceptorList、interceptorIndex
</span><span class="cm">            	handler：浏览器发送的请求所匹配的控制器方法
</span><span class="cm">            	interceptorList：处理控制器方法的所有拦截器集合
</span><span class="cm">            	interceptorIndex：拦截器索引，控制拦截器afterCompletion()的执行
</span><span class="cm">            */</span>
            <span class="n">mappedHandler</span> <span class="o">=</span> <span class="n">getHandler</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">);</span>
            <span class="k">if</span> <span class="o">(</span><span class="n">mappedHandler</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">noHandlerFound</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
                <span class="k">return</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c1">// Determine handler adapter for the current request.
</span><span class="c1"></span>           	<span class="c1">// 通过控制器方法创建相应的处理器适配器，调用所对应的控制器方法
</span><span class="c1"></span>            <span class="n">HandlerAdapter</span> <span class="n">ha</span> <span class="o">=</span> <span class="n">getHandlerAdapter</span><span class="o">(</span><span class="n">mappedHandler</span><span class="o">.</span><span class="na">getHandler</span><span class="o">());</span>

            <span class="c1">// Process last-modified header, if supported by the handler.
</span><span class="c1"></span>            <span class="n">String</span> <span class="n">method</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="na">getMethod</span><span class="o">();</span>
            <span class="kt">boolean</span> <span class="n">isGet</span> <span class="o">=</span> <span class="s">&#34;GET&#34;</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">method</span><span class="o">);</span>
            <span class="k">if</span> <span class="o">(</span><span class="n">isGet</span> <span class="o">||</span> <span class="s">&#34;HEAD&#34;</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">method</span><span class="o">))</span> <span class="o">{</span>
                <span class="kt">long</span> <span class="n">lastModified</span> <span class="o">=</span> <span class="n">ha</span><span class="o">.</span><span class="na">getLastModified</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">mappedHandler</span><span class="o">.</span><span class="na">getHandler</span><span class="o">());</span>
                <span class="k">if</span> <span class="o">(</span><span class="k">new</span> <span class="n">ServletWebRequest</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">).</span><span class="na">checkNotModified</span><span class="o">(</span><span class="n">lastModified</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="n">isGet</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">return</span><span class="o">;</span>
                <span class="o">}</span>
            <span class="o">}</span>
			
            <span class="c1">// 调用拦截器的preHandle()
</span><span class="c1"></span>            <span class="k">if</span> <span class="o">(!</span><span class="n">mappedHandler</span><span class="o">.</span><span class="na">applyPreHandle</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">))</span> <span class="o">{</span>
                <span class="k">return</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c1">// Actually invoke the handler.
</span><span class="c1"></span>            <span class="c1">// 由处理器适配器调用具体的控制器方法，最终获得ModelAndView对象
</span><span class="c1"></span>            <span class="n">mv</span> <span class="o">=</span> <span class="n">ha</span><span class="o">.</span><span class="na">handle</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">mappedHandler</span><span class="o">.</span><span class="na">getHandler</span><span class="o">());</span>

            <span class="k">if</span> <span class="o">(</span><span class="n">asyncManager</span><span class="o">.</span><span class="na">isConcurrentHandlingStarted</span><span class="o">())</span> <span class="o">{</span>
                <span class="k">return</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="n">applyDefaultViewName</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">mv</span><span class="o">);</span>
            <span class="c1">// 调用拦截器的postHandle()
</span><span class="c1"></span>            <span class="n">mappedHandler</span><span class="o">.</span><span class="na">applyPostHandle</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">mv</span><span class="o">);</span>
        <span class="o">}</span>
        <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">dispatchException</span> <span class="o">=</span> <span class="n">ex</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">catch</span> <span class="o">(</span><span class="n">Throwable</span> <span class="n">err</span><span class="o">)</span> <span class="o">{</span>
            <span class="c1">// As of 4.3, we&#39;re processing Errors thrown from handler methods as well,
</span><span class="c1"></span>            <span class="c1">// making them available for @ExceptionHandler methods and other scenarios.
</span><span class="c1"></span>            <span class="n">dispatchException</span> <span class="o">=</span> <span class="k">new</span> <span class="n">NestedServletException</span><span class="o">(</span><span class="s">&#34;Handler dispatch failed&#34;</span><span class="o">,</span> <span class="n">err</span><span class="o">);</span>
        <span class="o">}</span>
        <span class="c1">// 后续处理：处理模型数据和渲染视图
</span><span class="c1"></span>        <span class="n">processDispatchResult</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">mappedHandler</span><span class="o">,</span> <span class="n">mv</span><span class="o">,</span> <span class="n">dispatchException</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">triggerAfterCompletion</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">mappedHandler</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
    <span class="o">}</span>
    <span class="k">catch</span> <span class="o">(</span><span class="n">Throwable</span> <span class="n">err</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">triggerAfterCompletion</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">mappedHandler</span><span class="o">,</span>
                               <span class="k">new</span> <span class="n">NestedServletException</span><span class="o">(</span><span class="s">&#34;Handler processing failed&#34;</span><span class="o">,</span> <span class="n">err</span><span class="o">));</span>
    <span class="o">}</span>
    <span class="k">finally</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">asyncManager</span><span class="o">.</span><span class="na">isConcurrentHandlingStarted</span><span class="o">())</span> <span class="o">{</span>
            <span class="c1">// Instead of postHandle and afterCompletion
</span><span class="c1"></span>            <span class="k">if</span> <span class="o">(</span><span class="n">mappedHandler</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">mappedHandler</span><span class="o">.</span><span class="na">applyAfterConcurrentHandlingStarted</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">else</span> <span class="o">{</span>
            <span class="c1">// Clean up any resources used by a multipart request.
</span><span class="c1"></span>            <span class="k">if</span> <span class="o">(</span><span class="n">multipartRequestParsed</span><span class="o">)</span> <span class="o">{</span>
                <span class="n">cleanupMultipart</span><span class="o">(</span><span class="n">processedRequest</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="dprocessdispatchresult">d&gt;processDispatchResult()</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="kd">private</span> <span class="kt">void</span> <span class="nf">processDispatchResult</span><span class="o">(</span><span class="n">HttpServletRequest</span> <span class="n">request</span><span class="o">,</span> <span class="n">HttpServletResponse</span> <span class="n">response</span><span class="o">,</span>
                                   <span class="nd">@Nullable</span> <span class="n">HandlerExecutionChain</span> <span class="n">mappedHandler</span><span class="o">,</span> <span class="nd">@Nullable</span> <span class="n">ModelAndView</span> <span class="n">mv</span><span class="o">,</span>
                                   <span class="nd">@Nullable</span> <span class="n">Exception</span> <span class="n">exception</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>

    <span class="kt">boolean</span> <span class="n">errorView</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>

    <span class="k">if</span> <span class="o">(</span><span class="n">exception</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">exception</span> <span class="k">instanceof</span> <span class="n">ModelAndViewDefiningException</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;ModelAndViewDefiningException encountered&#34;</span><span class="o">,</span> <span class="n">exception</span><span class="o">);</span>
            <span class="n">mv</span> <span class="o">=</span> <span class="o">((</span><span class="n">ModelAndViewDefiningException</span><span class="o">)</span> <span class="n">exception</span><span class="o">).</span><span class="na">getModelAndView</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="k">else</span> <span class="o">{</span>
            <span class="n">Object</span> <span class="n">handler</span> <span class="o">=</span> <span class="o">(</span><span class="n">mappedHandler</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">mappedHandler</span><span class="o">.</span><span class="na">getHandler</span><span class="o">()</span> <span class="o">:</span> <span class="kc">null</span><span class="o">);</span>
            <span class="n">mv</span> <span class="o">=</span> <span class="n">processHandlerException</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="n">handler</span><span class="o">,</span> <span class="n">exception</span><span class="o">);</span>
            <span class="n">errorView</span> <span class="o">=</span> <span class="o">(</span><span class="n">mv</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="c1">// Did the handler return a view to render?
</span><span class="c1"></span>    <span class="k">if</span> <span class="o">(</span><span class="n">mv</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">mv</span><span class="o">.</span><span class="na">wasCleared</span><span class="o">())</span> <span class="o">{</span>
        <span class="c1">// 处理模型数据和渲染视图
</span><span class="c1"></span>        <span class="n">render</span><span class="o">(</span><span class="n">mv</span><span class="o">,</span> <span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">);</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">errorView</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">WebUtils</span><span class="o">.</span><span class="na">clearErrorRequestAttributes</span><span class="o">(</span><span class="n">request</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>
    <span class="k">else</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">logger</span><span class="o">.</span><span class="na">isTraceEnabled</span><span class="o">())</span> <span class="o">{</span>
            <span class="n">logger</span><span class="o">.</span><span class="na">trace</span><span class="o">(</span><span class="s">&#34;No view rendering, null ModelAndView returned.&#34;</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>

    <span class="k">if</span> <span class="o">(</span><span class="n">WebAsyncUtils</span><span class="o">.</span><span class="na">getAsyncManager</span><span class="o">(</span><span class="n">request</span><span class="o">).</span><span class="na">isConcurrentHandlingStarted</span><span class="o">())</span> <span class="o">{</span>
        <span class="c1">// Concurrent handling started during a forward
</span><span class="c1"></span>        <span class="k">return</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="k">if</span> <span class="o">(</span><span class="n">mappedHandler</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// Exception (if any) is already handled..
</span><span class="c1"></span>        <span class="c1">// 调用拦截器的afterCompletion()
</span><span class="c1"></span>        <span class="n">mappedHandler</span><span class="o">.</span><span class="na">triggerAfterCompletion</span><span class="o">(</span><span class="n">request</span><span class="o">,</span> <span class="n">response</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4springmvc的执行流程">4、SpringMVC的执行流程</h3>
<ol>
<li>
<p>用户向服务器发送请求，请求被SpringMVC 前端控制器 DispatcherServlet捕获。</p>
</li>
<li>
<p>DispatcherServlet对请求URL进行解析，得到请求资源标识符（URI），判断请求URI对应的映射：</p>
</li>
</ol>
<p>a) 不存在</p>
<p>i. 再判断是否配置了mvc:default-servlet-handler</p>
<p>ii. 如果没配置，则控制台报映射查找不到，客户端展示404错误</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img006.png"
        data-srcset="img/img006.png, img/img006.png 1.5x, img/img006.png 2x"
        data-sizes="auto"
        alt="img/img006.png"
        title="image-20210709214911404" /></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img007.png"
        data-srcset="img/img007.png, img/img007.png 1.5x, img/img007.png 2x"
        data-sizes="auto"
        alt="img/img007.png"
        title="image-20210709214947432" /></p>
<p>iii. 如果有配置，则访问目标资源（一般为静态资源，如：JS,CSS,HTML），找不到客户端也会展示404错误</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img008.png"
        data-srcset="img/img008.png, img/img008.png 1.5x, img/img008.png 2x"
        data-sizes="auto"
        alt="img/img008.png"
        title="image-20210709215255693" /></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="img/img009.png"
        data-srcset="img/img009.png, img/img009.png 1.5x, img/img009.png 2x"
        data-sizes="auto"
        alt="img/img009.png"
        title="image-20210709215336097" /></p>
<p>b) 存在则执行下面的流程</p>
<ol start="3">
<li>
<p>根据该URI，调用HandlerMapping获得该Handler配置的所有相关的对象（包括Handler对象以及Handler对象对应的拦截器），最后以HandlerExecutionChain执行链对象的形式返回。</p>
</li>
<li>
<p>DispatcherServlet 根据获得的Handler，选择一个合适的HandlerAdapter。</p>
</li>
<li>
<p>如果成功获得HandlerAdapter，此时将开始执行拦截器的preHandler(…)方法【正向】</p>
</li>
<li>
<p>提取Request中的模型数据，填充Handler入参，开始执行Handler（Controller)方法，处理请求。在填充Handler的入参过程中，根据你的配置，Spring将帮你做一些额外的工作：</p>
</li>
</ol>
<p>a) HttpMessageConveter： 将请求消息（如Json、xml等数据）转换成一个对象，将对象转换为指定的响应信息</p>
<p>b) 数据转换：对请求消息进行数据转换。如String转换成Integer、Double等</p>
<p>c) 数据格式化：对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等</p>
<p>d) 数据验证： 验证数据的有效性（长度、格式等），验证结果存储到BindingResult或Error中</p>
<ol start="7">
<li>
<p>Handler执行完成后，向DispatcherServlet 返回一个ModelAndView对象。</p>
</li>
<li>
<p>此时将开始执行拦截器的postHandle(&hellip;)方法【逆向】。</p>
</li>
<li>
<p>根据返回的ModelAndView（此时会判断是否存在异常：如果存在异常，则执行HandlerExceptionResolver进行异常处理）选择一个适合的ViewResolver进行视图解析，根据Model和View，来渲染视图。</p>
</li>
<li>
<p>渲染视图完毕执行拦截器的afterCompletion(…)方法【逆向】。</p>
</li>
<li>
<p>将渲染结果返回给客户端。</p>
</li>
</ol>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2021-06-30</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/posts/springmvc/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC" data-via="xxxx" data-hashtags="java,SpringMVC,模块化规范"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="http://example.org/posts/springmvc/" data-hashtag="java"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC"><i data-svg-src="/lib/simple-icons/icons/line.min.svg"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC" data-description=""><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC" data-description=""><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="http://example.org/posts/springmvc/" data-title="SpringMVC"><i class="fab fa-evernote fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/java/">java</a>,&nbsp;<a href="/tags/springmvc/">SpringMVC</a>,&nbsp;<a href="/tags/%E6%A8%A1%E5%9D%97%E5%8C%96%E8%A7%84%E8%8C%83/">模块化规范</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/mysql/" class="prev" rel="prev" title="mysql"><i class="fas fa-angle-left fa-fw"></i>mysql</a>
            <a href="/posts/htmlcss%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" class="next" rel="next" title="html&amp;css基础知识">html&amp;css基础知识<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.89.1">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://github.com/yzuxqz" target="_blank">yzuxqz</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript" src="/lib/katex/katex.min.js"></script><script type="text/javascript" src="/lib/katex/auto-render.min.js"></script><script type="text/javascript" src="/lib/katex/copy-tex.min.js"></script><script type="text/javascript" src="/lib/katex/mhchem.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
